我有一个简单的RecyclerView,每个项目只有一个TextView。通过在MainActivity中使用recyclerView.smoothScrollToPosition(myPosition);
,我可以滚动到自定义项目。我需要的是在滚动后立即更改滚动到的当前项目的textColor。
例如,如果myPosition为3,而RecyclerView的第3个项目为“当前”或“聚焦”,则我想更改此第3个项目的textColor。
我该怎么做?
MainActivity.java:
public class MainActivity extends AppCompatActivity implements MyAdapter.ItemClickListener {
MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> itemTitle_List = new ArrayList<>();
itemTitle_List.add("facebook");
itemTitle_List.add("flickr");
itemTitle_List.add("google_plus");
itemTitle_List.add("instagram");
itemTitle_List.add("linkedin");
itemTitle_List.add("pinterest");
itemTitle_List.add("soundcloud");
itemTitle_List.add("swarm");
itemTitle_List.add("tumblr");
itemTitle_List.add("twitter");
// set up the RecyclerView
RecyclerView recyclerView = findViewById(R.id.recycler_view);
RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
adapter = new MyAdapter(this, itemTitle_List);
adapter.setClickListener(this);
recyclerView.setAdapter(adapter);
recyclerView.smoothScrollToPosition(3);
}
@Override
public void onItemClick(View view, int position) {
//Toast.makeText(this, "You clicked " + adapter.getItem(position) + " on row number " + position, Toast.LENGTH_SHORT).show();
}
}
MyAdapter.java:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<String> itemTitle_List;
private LayoutInflater layoutInflater;
private ItemClickListener itemClickListener;
// data is passed into the constructor
MyAdapter(Context context, List<String> itemTitle_List) {
this.layoutInflater = LayoutInflater.from(context);
this.itemTitle_List = itemTitle_List;
}
// inflates the row layout from xml when needed
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = layoutInflater.inflate(R.layout.recyclerview_row, parent, false);
return new ViewHolder(view);
}
// total number of rows
@Override
public int getItemCount() {
return itemTitle_List.size();
}
// binds the title to the TextView, and image to the ImageView in each row
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
String title = itemTitle_List.get(position);
holder.textView.setText(title);
}
// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView textView;
ViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.text_view);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (itemClickListener != null) itemClickListener.onItemClick(view, getAdapterPosition());
}
}
// convenience method for getting data at click position
String getItem(int id) {
return itemTitle_List.get(id);
}
// allows clicks events to be caught
void setClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
// parent activity will implement this method to respond to click events
public interface ItemClickListener {
void onItemClick(View view, int position);
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler_view">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
recyclerview_row.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="80dp"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_view"
android:layout_gravity="center_vertical"
android:layout_margin="10dp"/>
</LinearLayout>
答案 0 :(得分:0)
我找到了一个简单的解决方案,但是我不确定这是否是标准的。
在 MyAdapter.java 中添加一个名为 row_index = 0 的公共整数和一个公共函数,如下所示:
public void selectRow(int index){
row_index=index;
notifyDataSetChanged();
}
然后在 MyAdapter.java 中的 onBindViewHolder 函数中,添加以下代码:
if(position==row_index){
holder.textView.setTextColor(Color.RED);
}
else{
holder.textView.setTextColor(Color.BLACK);
}
然后,在 MainActivity.java 中的recyclerView.smoothScrollToPosition(3)
之后,使用此命令:
MyAdapter.selectRow(3);
MyAdapter.notifyDataSetChanged();
这样,当RecyclerView滚动到第三项时,该项目的textColor变为红色。