我创建了一个充当菜单的列表视图。当用户选择特定菜单时,另一个片段将显示在右侧。
请查看此图片: 问题是,图标和标题仅高亮显示,而不是整个选定行。如何突出显示所选行,而不是仅突出显示图标和标题?
这是我的代码:
fragment_inventory_list.xml
"QUERY PLAN"
"Limit (cost=974652.20..974652.22 rows=10 width=54) (actual time=2817.579..2825.071 rows=10 loops=1)"
" Buffers: shared hit=120299 read=573195"
" -> Sort (cost=974652.20..974666.79 rows=5839 width=54) (actual time=2817.578..2817.578 rows=10 loops=1)"
" Sort Key: (count(DISTINCT A.id)) DESC"
" Sort Method: top-N heapsort Memory: 26kB"
" Buffers: shared hit=120299 read=573195"
" -> GroupAggregate (cost=974325.65..974526.02 rows=5839 width=54) (actual time=2792.465..2817.097 rows=3618 loops=1)"
" Group Key: C.title"
" Buffers: shared hit=120299 read=573195"
" -> Sort (cost=974325.65..974372.97 rows=18931 width=32) (actual time=2792.451..2795.161 rows=45175 loops=1)"
" Sort Key: C.title"
" Sort Method: quicksort Memory: 5055kB"
" Buffers: shared hit=120299 read=573195"
" -> Gather (cost=968845.30..972980.74 rows=18931 width=32) (actual time=2753.402..2778.648 rows=45175 loops=1)"
" Workers Planned: 1"
" Workers Launched: 1"
" Buffers: shared hit=120299 read=573195"
" -> Parallel Hash Join (cost=967845.30..970087.64 rows=11136 width=32) (actual time=2751.725..2764.832 rows=22588 loops=2)"
" Hash Cond: ((C.id)::text = (A.id)::text)"
" Buffers: shared hit=120299 read=573195"
" -> Parallel Seq Scan on C (cost=0.00..1945.87 rows=66687 width=32) (actual time=0.017..4.316 rows=56684 loops=2)"
" Buffers: shared read=1279"
" -> Parallel Hash (cost=966604.55..966604.55 rows=99260 width=9) (actual time=2750.987..2750.987 rows=20950 loops=2)"
" Buckets: 262144 Batches: 1 Memory Usage: 4032kB"
" Buffers: shared hit=120266 read=571904"
" -> Nested Loop (cost=219572.23..966604.55 rows=99260 width=9) (actual time=665.832..2744.270 rows=20950 loops=2)"
" Buffers: shared hit=120266 read=571904"
" -> Parallel Hash Join (cost=219571.79..917516.91 rows=99260 width=4) (actual time=665.804..2583.675 rows=20950 loops=2)"
" Hash Cond: ((D.app)::text = (B.app)::text)"
" Buffers: shared hit=8 read=524214"
" -> Parallel Bitmap Heap Scan on D (cost=217542.51..895848.77 rows=5126741 width=13) (actual time=661.254..1861.862 rows=6160441 loops=2)"
" Recheck Cond: ((action_type)::text = ANY ('{10,11}'::text[]))"
" Heap Blocks: exact=242152"
" Buffers: shared hit=3 read=523925"
" -> Bitmap Index Scan on D_index_action_type (cost=0.00..214466.46 rows=12304178 width=0) (actual time=546.470..546.471 rows=12320882 loops=1)"
" Index Cond: ((action_type)::text = ANY ('{10,11}'::text[]))"
" Buffers: shared hit=3 read=33669"
" -> Parallel Hash (cost=1859.36..1859.36 rows=13594 width=12) (actual time=4.337..4.337 rows=16313 loops=2)"
" Buckets: 32768 Batches: 1 Memory Usage: 1152kB"
" Buffers: shared hit=5 read=289"
" -> Parallel Index Only Scan using B_index_app on B (cost=0.29..1859.36 rows=13594 width=12) (actual time=0.015..2.218 rows=16313 loops=2)"
" Heap Fetches: 0"
" Buffers: shared hit=5 read=289"
" -> Index Scan using A_index_number on A (cost=0.43..0.48 rows=1 width=24) (actual time=0.007..0.007 rows=1 loops=41900)"
" Index Cond: ((number)::text = (D.number)::text)"
" Buffers: shared hit=120258 read=47690"
"Planning Time: 0.747 ms"
"Execution Time: 2825.118 ms"
InventoryListFragment.java
<?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"
tools:context=".InventoryListFragment"
android:layout_marginTop="@dimen/padding_50"
android:orientation="vertical"
android:background="?android:attr/activatedBackgroundIndicator">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="@+id/inventorylist_listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:choiceMode="singleChoice"
android:listSelector="@color/light_gray"/>
</LinearLayout>
</LinearLayout>
答案 0 :(得分:0)
在适配器getView()
中,只需添加点击监听器并设置背景色
@Override
public View getView(int position, View convertView, ViewGroup parent) {
....
....
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
view.setBackgroundColor(Color.GREY);
}
});
....
....
return view;
}
答案 1 :(得分:0)
尝试一下
private class InventoryListAdapter extends ... {
private int focusedPosition = -1; // -1 means nothing is selected
....
....
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if (position == focusedPosition) {
view.setBackgroundColor(/* your selected color */);
} else {
view.setBackgroundColor(/* your NOT selected color */);
}
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
focusedPosition = position;
notifyDataSetChanged();
}
});
return view;
}
答案 2 :(得分:0)
我建议在recyclerview
上使用listview
,就像@Ferran已经回答listview
我想为recyclerview
public class AdapterClass extends RecyclerView.Adapter<AdapterClass.ViewHolder> {
private int selected_position = -1;
@Override
public void onBindViewHolder(PlacesLocationAdapter.ViewHolder holder, final int position) {
if (selected_position == position) {
// do your stuff here like
//Change selected item background color and Show sub item views
} else {
// do your stuff here like
//Change unselected item background color and Hide sub item views
}
// rest of the code here
holder.linelayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(selected_position==position){
selected_position=-1;
notifyDataSetChanged();
return;
}
selected_position = position;
notifyDataSetChanged();
}
});
//rest of the code here
}
}
答案 3 :(得分:0)
在InventoryList
模型中,添加另一个字段,例如isSelected。
String isSelected;
public String getIsSelected() {
return isSelected;
}
public void setIsSelected(String isSelected) {
this.isSelected = isSelected;
}
然后默认情况下,设置isSelected字段的值0
。如果要默认打开产品片段,则其值为1
。
list.add(new InventoryList(R.drawable.ic_product,"Products","1"));
list.add(new InventoryList(R.drawable.ic_services,"Services","0"));
list.add(new InventoryList(R.drawable.ic_category, "Categories","0"));
list.add(new InventoryList(R.drawable.ic_local_offer,"Discounts","0"));
lvinventory.setAdapter(adapter);
lvinventory.setOnItemClickListener(this);
在onItemClick
函数中编写以下代码:
for (int i = 0; i < list.size(); i++) {
list.get(i).setIsSelected("0");
}
selectedList.setIsSelected("1");
adapter.notifyDataSetChanged();
在“ InventoryListAdapter”文件中,使用“ getView”覆盖功能来设置背景颜色,例如...
if (list.get(position).getIsSelected().equals("1")) {
view.setBackgroundColor(/* your selected color */); // Gray
} else {
view.setBackgroundColor(/* your NOT selected color */); // White
}