mirror of https://github.com/FongMi/TV.git
parent
4f108806ca
commit
fa0245e5dc
@ -0,0 +1,79 @@ |
||||
package com.fongmi.bear.ui.adapter; |
||||
|
||||
import android.view.LayoutInflater; |
||||
import android.view.View; |
||||
import android.view.ViewGroup; |
||||
|
||||
import androidx.annotation.NonNull; |
||||
import androidx.recyclerview.widget.RecyclerView; |
||||
|
||||
import com.fongmi.bear.bean.Site; |
||||
import com.fongmi.bear.databinding.AdapterSiteBinding; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
public class SiteAdapter extends RecyclerView.Adapter<SiteAdapter.ViewHolder> { |
||||
|
||||
private OnItemClickListener mListener; |
||||
private List<Site> mItems; |
||||
|
||||
public SiteAdapter() { |
||||
mItems = new ArrayList<>(); |
||||
} |
||||
|
||||
public interface OnItemClickListener { |
||||
void onItemClick(Site item); |
||||
} |
||||
|
||||
public void setOnItemClickListener(OnItemClickListener listener) { |
||||
this.mListener = listener; |
||||
} |
||||
|
||||
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { |
||||
|
||||
private final AdapterSiteBinding binding; |
||||
|
||||
public ViewHolder(@NonNull AdapterSiteBinding binding) { |
||||
super(binding.getRoot()); |
||||
this.binding = binding; |
||||
itemView.setOnClickListener(this); |
||||
} |
||||
|
||||
@Override |
||||
public void onClick(View view) { |
||||
Site item = mItems.get(getLayoutPosition()); |
||||
setHome(getLayoutPosition()); |
||||
mListener.onItemClick(item); |
||||
} |
||||
} |
||||
|
||||
private void setHome(int position) { |
||||
for (int i = 0; i < mItems.size(); i++) mItems.get(i).setHome(i == position); |
||||
notifyItemRangeChanged(0, mItems.size()); |
||||
} |
||||
|
||||
public void addAll(List<Site> items) { |
||||
mItems.clear(); |
||||
mItems.addAll(items); |
||||
notifyDataSetChanged(); |
||||
} |
||||
|
||||
@Override |
||||
public int getItemCount() { |
||||
return mItems.size(); |
||||
} |
||||
|
||||
@NonNull |
||||
@Override |
||||
public SiteAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { |
||||
return new ViewHolder(AdapterSiteBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false)); |
||||
} |
||||
|
||||
@Override |
||||
public void onBindViewHolder(@NonNull SiteAdapter.ViewHolder holder, int position) { |
||||
Site item = mItems.get(position); |
||||
holder.itemView.setSelected(item.isHome()); |
||||
holder.binding.name.setText(item.getName()); |
||||
} |
||||
} |
||||
@ -1,6 +1,6 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<selector xmlns:android="http://schemas.android.com/apk/res/android"> |
||||
<item android:drawable="@drawable/shape_item_selected" android:state_selected="true" /> |
||||
<item android:drawable="@drawable/shape_item_selected" android:state_focused="true" /> |
||||
<item android:drawable="@drawable/shape_item_focused" android:state_selected="true" /> |
||||
<item android:drawable="@drawable/shape_item_focused" android:state_focused="true" /> |
||||
<item android:drawable="@drawable/shape_item_normal" /> |
||||
</selector> |
||||
@ -0,0 +1,6 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<selector xmlns:android="http://schemas.android.com/apk/res/android"> |
||||
<item android:drawable="@drawable/shape_site_selected" android:state_selected="true" /> |
||||
<item android:drawable="@drawable/shape_site_focused" android:state_focused="true" /> |
||||
<item android:drawable="@drawable/shape_site_normal" /> |
||||
</selector> |
||||
@ -1,6 +1,6 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<selector xmlns:android="http://schemas.android.com/apk/res/android"> |
||||
<item android:drawable="@drawable/shape_vod_selected" android:state_selected="true" /> |
||||
<item android:drawable="@drawable/shape_vod_selected" android:state_focused="true" /> |
||||
<item android:drawable="@drawable/shape_vod_focused" android:state_selected="true" /> |
||||
<item android:drawable="@drawable/shape_vod_focused" android:state_focused="true" /> |
||||
<item android:drawable="@drawable/shape_vod_normal" /> |
||||
</selector> |
||||
@ -0,0 +1,15 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<shape xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:shape="rectangle"> |
||||
|
||||
<solid android:color="@color/black_60" /> |
||||
|
||||
<corners android:radius="4dp" /> |
||||
|
||||
<padding |
||||
android:bottom="8dp" |
||||
android:left="12dp" |
||||
android:right="12dp" |
||||
android:top="8dp" /> |
||||
|
||||
</shape> |
||||
@ -0,0 +1,15 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<shape xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:shape="rectangle"> |
||||
|
||||
<solid android:color="@color/black_50" /> |
||||
|
||||
<corners android:radius="4dp" /> |
||||
|
||||
<padding |
||||
android:bottom="8dp" |
||||
android:left="12dp" |
||||
android:right="12dp" |
||||
android:top="8dp" /> |
||||
|
||||
</shape> |
||||
@ -0,0 +1,15 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<shape xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:shape="rectangle"> |
||||
|
||||
<solid android:color="@color/blue_500" /> |
||||
|
||||
<corners android:radius="4dp" /> |
||||
|
||||
<padding |
||||
android:bottom="8dp" |
||||
android:left="12dp" |
||||
android:right="12dp" |
||||
android:top="8dp" /> |
||||
|
||||
</shape> |
||||
@ -0,0 +1,22 @@ |
||||
<?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" |
||||
android:layout_marginTop="6dp" |
||||
android:layout_marginBottom="6dp" |
||||
android:background="@drawable/selector_site" |
||||
android:focusable="true" |
||||
android:focusableInTouchMode="true"> |
||||
|
||||
<TextView |
||||
android:id="@+id/name" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:gravity="center" |
||||
android:singleLine="true" |
||||
android:textColor="@color/white" |
||||
android:textSize="18sp" |
||||
tools:text="泥巴影院" /> |
||||
|
||||
</LinearLayout> |
||||
@ -0,0 +1,13 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:orientation="vertical" |
||||
android:padding="16dp"> |
||||
|
||||
<androidx.recyclerview.widget.RecyclerView |
||||
android:id="@+id/site" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" /> |
||||
|
||||
</LinearLayout> |
||||
Loading…
Reference in new issue