package com.android_examples.recyclerview_android_examplescom;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
Context context;
RecyclerView recyclerView;
LinearLayout relativeLayout;
Button button;
RecyclerView.Adapter recyclerViewAdapter;
RecyclerView.LayoutManager recylerViewLayoutManager;
String[] subjects =
{
"ANDROID",
"PHP",
"BLOGGER",
"WORDPRESS",
"JOOMLA",
"ASP.NET",
"JAVA",
"C++",
"MATHS",
"HINDI",
"ENGLISH"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_main);
context = getApplicationContext();
relativeLayout = (LinearLayout) findViewById(R.id.relativelayout1);
button = (Button) findViewById(R.id.button);
recyclerView = (RecyclerView) findViewById(R.id.recyclerview1);
recylerViewLayoutManager = new LinearLayoutManager(context);
recyclerView.setLayoutManager(recylerViewLayoutManager);
recyclerViewAdapter = new RecyclerViewAdapter(context, subjects);
recyclerView.setAdapter(recyclerViewAdapter);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
recylerViewLayoutManager.findViewByPosition(10).setBackgroundColor(Color.BLUE);
}
});
}
}
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="vertical"
tools:context="com.android_examples.recyclerview_android_examplescom.MainActivity"
android:id="@+id/relativelayout1">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Change"
android:id="@+id/button"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview1"
android:scrollbars="vertical"
android:layout_marginTop="20dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
RecyclerAdapter: -
package com.android_examples.recyclerview_android_examplescom;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
String[] SubjectValues;
Context context;
View view1;
ViewHolder viewHolder1;
TextView textView;
public RecyclerViewAdapter(Context context1,String[] SubjectValues1){
SubjectValues = SubjectValues1;
context = context1;
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public TextView textView;
public ViewHolder(View v){
super(v);
textView = (TextView)v.findViewById(R.id.subject_textview);
}
}
@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
view1 = LayoutInflater.from(context).inflate(R.layout.recyclerview_items,parent,false);
viewHolder1 = new ViewHolder(view1);
return viewHolder1;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position){
package com.android_examples.recyclerview_android_examplescom;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
String[] SubjectValues;
Context context;
View view1;
ViewHolder viewHolder1;
TextView textView;
public RecyclerViewAdapter(Context context1,String[] SubjectValues1){
SubjectValues = SubjectValues1;
context = context1;
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public TextView textView;
public ViewHolder(View v){
super(v);
textView = (TextView)v.findViewById(R.id.subject_textview);
}
}
@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
view1 = LayoutInflater.from(context).inflate(R.layout.recyclerview_items,parent,false);
viewHolder1 = new ViewHolder(view1);
return viewHolder1;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position){
holder.textView.setText(SubjectValues[position]);
}
@Override
public int getItemCount(){
return SubjectValues.length;
}
}
holder.textView.setText(SubjectValues[position]);
}
@Override
public int getItemCount(){
return SubjectValues.length;
}
}
如果我想从按钮单击更改Recyclerview中的特定项目的背景,它将仅对可见项目产生影响,而不是对于不可见的方式(第一项对用户可见,它将影响,但对于最后一项,它不会改变颜色)
请帮助我......
提前致谢..
答案 0 :(得分:0)
首先在此设置视图下获取convertView(视图对象)的单击操作
if(!convertView.isSelected()){
convertView.setSelected(true);
//here change the background color as selected.
}else{
convertView.setSelected(false);
//here change the background color as unselected.
}
亨德尔在选拔中被选为现状。
如何获取convertView对象引用
答案 1 :(得分:0)
OnItemClickListener
将为您提供视图的位置,您可以使用以下代码更改所选定位行的背景
private View SelectedItem;
private void BackColor(View view) {
if (SelectedItem != null) {
SelectedItem.setBackgroundColor(Color.TRANSPARENT);
}
view.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
SelectedItem = view;
}
yourListview.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick (AdapterView < ? > adapterView, View view,int i, long l){
BackColor(view);
}
}