Reputation: 229
How can we set like different background color, font color for only 1 item in listview based on selected (clicked) item?
I want to make active button style upon clicking listview item, can we do that?
I am able to get the value of the clicked item, then how can we set that listview item to behave different than other item?
Upvotes: 0
Views: 148
Reputation: 6405
You can follow the steps below:
1) First, Create a xml for single row of ListView
(single_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="match_parent"
android:id="@+id/llList"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:text="TextView" />
</LinearLayout>
2) Create a Custom Adapter for ListView
:
public class CustomListAdapter extends BaseAdapter{
String [] text;
Context context;
int selectedPosition;
private static LayoutInflater inflater=null;
public CustomListAdapter(Context context, String[] text, int selectedPosition) {
// TODO Auto-generated constructor stub
this.context = context;
this.text = text;
this.selectedPosition = selectedPosition;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder
{
TextView tv;
LinearLayout llList;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.single_row, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.llList=(LinearLayout) rowView.findViewById(R.id.llList);
holder.tv.setText(text[position]);
holder.llList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.llList.setBackgroundColor(Color.CYAN);
}
});
return rowView;
}
}
3) Set This Custom Adapter to your ListView
in onCreate()
:
int selectedItem = 0; //Declare this Globally
lv=(ListView) findViewById(R.id.listView);
lv.setAdapter(new CustomListAdapter(this, yourStringList , selectedItem));
4) Then get the selected position of the ListView
through onItemSelectedListener:
and pass it to the adapter.
lv.setOnItemClickListener(new View.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View arg1, int position, long id) {
selectedItem = position;
CustomListAdapter.notifyDataSetChanged();
}
};
Hope this helps.
Upvotes: 1
Reputation: 883
You can keep track the position of the current selected element like below:
OnItemClickListener listViewOnItemClick = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View arg1, int position, long id) {
mSelectedItem = position;
mAdapter.notifyDataSetChanged();
}
};
And override the getView method of your adapter:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View view = View.inflate(context, R.layout.item_list, null);
if (position == mSelectedItem) {
// set your color
}
return view;
}
Upvotes: 0
Reputation: 423
define your own CheckableLinearLayout
implement Checkable
and override abstract
method.
let CheckableLinearLayout
be root layout for your item view. and then,make a selector for root view to add selected effects.
ListView
chooseMode --> singleMode
Upvotes: 0