開(kāi)篇
有一個(gè)需求,需要做一個(gè)左右雙向聯(lián)動(dòng)的列表波岛。類似于餓了么點(diǎn)餐頁(yè)面妙同,但是遠(yuǎn)沒(méi)有那么復(fù)雜射富,只是用于展示的一個(gè)頁(yè)面。只用了兩個(gè)RecycleView加適配器粥帚,沒(méi)有調(diào)用Fragment胰耗。
MainActivity
public class MainActivity extends Activity {
private RecyclerView mRvClass;
private RecyclerView mRvStudent;
private ClassAdapter classAdapter;
private StudentAdapter studentAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initAdapter();
}
private void initView() {
mRvClass = findViewById(R.id.rv_class);
mRvStudent = findViewById(R.id.rv_student);
}
private void initAdapter() {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this);
mRvClass.setLayoutManager(linearLayoutManager);
classAdapter = new ClassAdapter(MainActivity.this);
mRvClass.setAdapter(classAdapter);
//設(shè)置默認(rèn)的選取狀態(tài)
mRvClass.setScrollingTouchSlop(0);
LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(MainActivity.this);
mRvStudent.setLayoutManager(linearLayoutManager1);
studentAdapter = new StudentAdapter(MainActivity.this);
mRvStudent.setAdapter(studentAdapter);
/**
* 左側(cè)列表的事件處理
* */
classAdapter.setOnItemClickListener(new ClassAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
mRvClass.setScrollingTouchSlop(position);
studentAdapter.setData(position);
studentAdapter.notifyDataSetChanged();
}
});
}
}
activity_main
<?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="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_class"
android:layout_width="90dp"
android:layout_height="match_parent" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#363636" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_student"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="2dp" />
</LinearLayout>
ClassAdapter
public class ClassAdapter extends RecyclerView.Adapter<ClassAdapter.ViewHolder> {
private Context context;
private OnItemClickListener mOnItemClickListener = null;
private List<Boolean> isClicks;//控件是否被點(diǎn)擊,默認(rèn)為false,如果被點(diǎn)擊芒涡,改變值柴灯,控件根據(jù)值改變自身顏色
public ClassAdapter(Context context) {
this.context = context;
isClicks = new ArrayList<>();
for (int i = 0; i < 10; i++) {
isClicks.add(false);
if (i == 0) {
isClicks.set(0, true);
}
}
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.class_layout, parent, false);
ClassAdapter.ViewHolder holder = new ClassAdapter.ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
holder.itemView.setTag(holder.tv_class_name);
if (isClicks.get(position)) {
holder.tv_class_name.setTextColor(Color.parseColor("#ff0000"));
} else {
holder.tv_class_name.setTextColor(Color.parseColor("#000000"));
}
holder.tv_class_name.setText("年級(jí)" + position);
// 如果設(shè)置了回調(diào)卖漫,則設(shè)置點(diǎn)擊事件
if (mOnItemClickListener != null) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < isClicks.size(); i++) {
isClicks.set(i, false);
}
isClicks.set(position, true);
notifyDataSetChanged();
mOnItemClickListener.onItemClick(holder.itemView, position);
}
});
}
}
@Override
public int getItemCount() {
return 10;
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.mOnItemClickListener = onItemClickListener;
}
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView tv_class_name;
public ViewHolder(@NonNull View itemView) {
super(itemView);
tv_class_name = itemView.findViewById(R.id.tv_class_name);
}
}
}
StudentAdapter
public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.ViewHolder> {
private Context context;
private int i = 0;
public StudentAdapter(Context context) {
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.student_layout, parent, false);
StudentAdapter.ViewHolder holder = new StudentAdapter.ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.tv_name.setText("學(xué)生+" + i);
}
@Override
public int getItemCount() {
return 10;
}
public void setData(int position) {
this.i = position;
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView tv_name;
public ViewHolder(@NonNull View itemView) {
super(itemView);
tv_name = itemView.findViewById(R.id.tv_name);
}
}
}
(另加 RecycleView中Item的,點(diǎn)擊改變顏色赠群,點(diǎn)擊其他項(xiàng)改變顏色羊始,上一次點(diǎn)擊的變回原來(lái)的顏色)