2
- 我在我的应用程序中实现了Recyclerview,我需要在其项目上单击更新另一个listview,这就是为什么我在activity中实现了onClick事件。它工作正常。
- 但我还需要High Light RecyclerView选定的视图和其他都保持正常。
- 为此,我使用RecyclerView getChild方法使所有视图正常,然后选择视图突出显示。
- 我意识到它仅适用于RecyclerView的可见视图。 但我需要使正常的Recyclerview的所有视图,如果我用getChildAt与更大的位置,然后总可见计数它返回空指针异常。
- 登录,我得到
E /项目排名:29RecyclerView getChildAt(position)返回null
E /项目RV:15
E /儿童适配器:30
MainActivity.java
public class MainActivity extends Activity {
RecyclerView reView;
ArrayList<RvModel> rvModels;
revAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
reView = (RecyclerView) findViewById(R.id.reView);
reView.setLayoutManager(new GridLayoutManager(MainActivity.this, 3));
rvModels = new ArrayList<>();
adapter = new revAdapter(MainActivity.this, rvModels);
reView.setAdapter(adapter);
prepareData();
reView.addOnItemTouchListener(new RecyclerItemClickListener(MainActivity.this, reView, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Log.e("Item Pos", "" + position);
Log.e("Item Rv", "" + reView.getChildCount());
Log.e("Child Adapter", "" + reView.getAdapter().getItemCount());
for (int i = 0; i < reView.getChildCount(); i++) {
View v = reView.getChildAt(i);
TextView txtText = (TextView) v.findViewById(R.id.txtText);
txtText.setBackgroundColor(Color.WHITE);
txtText.setTextColor(Color.BLUE);
}
TextView txtText = (TextView) view.findViewById(R.id.txtText);
txtText.setBackgroundColor(Color.BLUE);
txtText.setTextColor(Color.WHITE);
}
@Override
public void onLongItemClick(View view, int position) {
}
}));
}
private void prepareData() {
rvModels.clear();
for (int i = 0; i < 30; i++) {
RvModel rvModel = new RvModel();
rvModel.setTitle("Item : " + i);
rvModels.add(rvModel);
}
adapter.notifyDataSetChanged();
}
}
revAdapter.java
public class revAdapter extends RecyclerView.Adapter<revAdapter.ViewHolder> {
Context context;
ArrayList<RvModel> rvModels;
LayoutInflater inflater;
public revAdapter(Context context, ArrayList<RvModel> rvModels) {
this.context = context;
this.rvModels = rvModels;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView ivIcon;
TextView txtText;
public ViewHolder(View itemView) {
super(itemView);
ivIcon = (ImageView) itemView.findViewById(R.id.ivIcon);
txtText = (TextView) itemView.findViewById(R.id.txtText);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = inflater.inflate(R.layout.rv_raw, parent, false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
RvModel rvModel = rvModels.get(position);
holder.txtText.setText(rvModel.getTitle());
}
@Override
public int getItemCount() {
return rvModels.size();
}
@Override
public int getItemViewType(int position) {
return position;
}
}
rv_raw.xml
<?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="match_parent"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<ImageView
android:id="@+id/ivIcon"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/txtText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
由于很好的解释。我也不想使用getItemViewType方法,但是每当我没有使用它时,Recycler视图在滚动时更新视图,而不是在本例中。它发生在另一个应用程序中,我使用回收查看网格布局。 –