mirror of https://github.com/FongMi/TV.git
parent
aa47c5fafe
commit
43b2cd8f66
@ -0,0 +1,67 @@ |
||||
package com.fongmi.android.tv.ui.adapter; |
||||
|
||||
import android.view.LayoutInflater; |
||||
import android.view.ViewGroup; |
||||
|
||||
import androidx.annotation.NonNull; |
||||
import androidx.recyclerview.widget.RecyclerView; |
||||
|
||||
import com.fongmi.android.tv.api.ApiConfig; |
||||
import com.fongmi.android.tv.databinding.AdapterDohBinding; |
||||
import com.github.catvod.bean.Doh; |
||||
|
||||
import java.util.List; |
||||
|
||||
public class DohAdapter extends RecyclerView.Adapter<DohAdapter.ViewHolder> { |
||||
|
||||
private final OnClickListener mListener; |
||||
private final List<Doh> mItems; |
||||
private int select; |
||||
|
||||
public DohAdapter(OnClickListener listener) { |
||||
this.mItems = ApiConfig.get().getDoh(); |
||||
this.mListener = listener; |
||||
} |
||||
|
||||
public void setSelect(int select) { |
||||
this.select = select; |
||||
} |
||||
|
||||
public int getSelect() { |
||||
return select; |
||||
} |
||||
|
||||
public interface OnClickListener { |
||||
|
||||
void onItemClick(Doh item); |
||||
} |
||||
|
||||
@Override |
||||
public int getItemCount() { |
||||
return mItems.size(); |
||||
} |
||||
|
||||
@NonNull |
||||
@Override |
||||
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { |
||||
return new ViewHolder(AdapterDohBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false)); |
||||
} |
||||
|
||||
@Override |
||||
public void onBindViewHolder(@NonNull ViewHolder holder, int position) { |
||||
Doh item = mItems.get(position); |
||||
holder.binding.text.setText(item.getName()); |
||||
holder.binding.text.setActivated(select == position); |
||||
holder.binding.text.setOnClickListener(v -> mListener.onItemClick(item)); |
||||
} |
||||
|
||||
public static class ViewHolder extends RecyclerView.ViewHolder { |
||||
|
||||
private final AdapterDohBinding binding; |
||||
|
||||
public ViewHolder(@NonNull AdapterDohBinding binding) { |
||||
super(binding.getRoot()); |
||||
this.binding = binding; |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,66 @@ |
||||
package com.fongmi.android.tv.ui.custom.dialog; |
||||
|
||||
import android.app.Activity; |
||||
import android.view.LayoutInflater; |
||||
import android.view.WindowManager; |
||||
|
||||
import androidx.appcompat.app.AlertDialog; |
||||
|
||||
import com.fongmi.android.tv.databinding.DialogDohBinding; |
||||
import com.fongmi.android.tv.impl.DohCallback; |
||||
import com.fongmi.android.tv.ui.adapter.DohAdapter; |
||||
import com.fongmi.android.tv.ui.custom.SpaceItemDecoration; |
||||
import com.fongmi.android.tv.utils.ResUtil; |
||||
import com.github.catvod.bean.Doh; |
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder; |
||||
|
||||
public class DohDialog implements DohAdapter.OnClickListener { |
||||
|
||||
private final DialogDohBinding binding; |
||||
private final DohCallback callback; |
||||
private final AlertDialog dialog; |
||||
private final DohAdapter adapter; |
||||
|
||||
public static DohDialog create(Activity activity) { |
||||
return new DohDialog(activity); |
||||
} |
||||
|
||||
public DohDialog index(int index) { |
||||
adapter.setSelect(index); |
||||
return this; |
||||
} |
||||
|
||||
public DohDialog(Activity activity) { |
||||
this.callback = (DohCallback) activity; |
||||
this.binding = DialogDohBinding.inflate(LayoutInflater.from(activity)); |
||||
this.dialog = new MaterialAlertDialogBuilder(activity).setView(binding.getRoot()).create(); |
||||
this.adapter = new DohAdapter(this); |
||||
} |
||||
|
||||
public void show() { |
||||
setRecyclerView(); |
||||
setDialog(); |
||||
} |
||||
|
||||
private void setRecyclerView() { |
||||
binding.recycler.setAdapter(adapter); |
||||
binding.recycler.setHasFixedSize(true); |
||||
binding.recycler.addItemDecoration(new SpaceItemDecoration(1, 16)); |
||||
binding.recycler.scrollToPosition(adapter.getSelect()); |
||||
} |
||||
|
||||
private void setDialog() { |
||||
if (adapter.getItemCount() == 0) return; |
||||
WindowManager.LayoutParams params = dialog.getWindow().getAttributes(); |
||||
params.width = (int) (ResUtil.getScreenWidth() * 0.4f); |
||||
dialog.getWindow().setAttributes(params); |
||||
dialog.getWindow().setDimAmount(0); |
||||
dialog.show(); |
||||
} |
||||
|
||||
@Override |
||||
public void onItemClick(Doh item) { |
||||
callback.setDoh(item); |
||||
dialog.dismiss(); |
||||
} |
||||
} |
||||
@ -0,0 +1,20 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:tools="http://schemas.android.com/tools" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content"> |
||||
|
||||
<TextView |
||||
android:id="@+id/text" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="40dp" |
||||
android:background="@drawable/selector_text" |
||||
android:focusable="true" |
||||
android:focusableInTouchMode="true" |
||||
android:gravity="center" |
||||
android:singleLine="true" |
||||
android:textColor="@color/text" |
||||
android:textSize="18sp" |
||||
tools:text="Google" /> |
||||
|
||||
</LinearLayout> |
||||
@ -0,0 +1,9 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<com.fongmi.android.tv.ui.custom.CustomRecyclerView xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:app="http://schemas.android.com/apk/res-auto" |
||||
android:id="@+id/recycler" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:padding="16dp" |
||||
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" |
||||
app:maxHeight="352dp" /> |
||||
@ -0,0 +1,8 @@ |
||||
package com.fongmi.android.tv.impl; |
||||
|
||||
import com.github.catvod.bean.Doh; |
||||
|
||||
public interface DohCallback { |
||||
|
||||
void setDoh(Doh doh); |
||||
} |
||||
Loading…
Reference in new issue