gpt4 book ai didi

android - ListView 以编程方式突出显示行项目

转载 作者:行者123 更新时间:2023-11-29 20:30:50 25 4
gpt4 key购买 nike

我有用于显示抽屉导航的 ListView ,并且我在 ListView 的 listselector 属性中设置了一个选择器 xml。现在,当我选择 ListView 项目时,列表项目突出显示。但我想以编程方式突出显示一个项目。我试过的代码:

列表行:

 <?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="wrap_content"
android:background="@drawable/navigation_list_selector">

>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="@dimen/drawer_section_height"
android:gravity="center"
android:layout_centerInParent="true"
android:text="boy"
android:fontFamily="sans-serif-light"
android:textColor="@color/navigation_text_color"
android:textSize="@dimen/menu_text_size"/>
</RelativeLayout>

选择器 XML:

   <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/nav_list_selectorshape1"/>
<item android:state_focused="true" android:drawable="@drawable/nav_list_selectorshape1" /> <!-- focused -->
<item android:state_pressed="true" android:drawable="@drawable/nav_list_selectorshape1" /> <!-- pressed -->

        <item android:state_selected="false" android:state_focused="false" android:state_pressed="false" android:drawable="@android:color/transparent" /> <!-- default -->
</selector>

列表 fragment :

public class NavigationMenuFragment extends ListFragment  {


private static final String TAG = NavigationMenuFragment.class.getName();
public NavigationMenuFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_nvigation_menu, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
List<String> listItems = new ArrayList<String>(Arrays.asList("SIGN OUT", "HOME", "MY DATA", "QW", "HISTORY", "SETTINGS"));
NavigationListAdapter navigationListAdapter = new NavigationListAdapter(getActivity(),listItems);
setListAdapter(navigationListAdapter);
}

@Override
public void onListItemClick(ListView lv, View v, int position, long id) {
android.support.v4.app.Fragment newContent = null;
switch (position) {
case 0:
Log.d(TAG,"Click on Fragment = "+position);
// newContent = new ColorFragment(android.R.color.holo_red_dark);
break;
case 1:
Log.d(TAG,"Click on Fragment = "+position);
// newContent = new ColorFragment(android.R.color.holo_green_dark);
break;
case 2:
Log.d(TAG,"Click on Fragment = "+position);
// newContent = new ColorFragment(android.R.color.holo_blue_bright);
break;
case 3:
Log.d(TAG,"Click on Fragment = "+position);
// newContent = new ColorFragment(android.R.color.white);
break;
case 4:
Log.d(TAG,"Click on Fragment = "+position);
// newContent = new ColorFragment(android.R.color.black);
break;
case 5:
Log.d(TAG,"Click on Fragment = "+position);
// newContent = new ColorFragment(android.R.color.white);
break;
case 6:
Log.d(TAG,"Click on Fragment = "+position);
// newContent = new ColorFragment(android.R.color.black);
break;
}
// if (newContent != null) {
if (getActivity() instanceof HomeActivity) {
HomeActivity fca = (HomeActivity) getActivity();
fca.switchContent(null);
}
// }
}

public void setHighlightedItem(int index){
((NavigationListAdapter) getListAdapter()).setmSelectedItemIndex(index);
}
}

适配器:

public class NavigationListAdapter extends BaseAdapter {

private List<String> mNavList;
private LayoutInflater mlayoutInflater;
private Context mContext;
private int mSelectedItemIndex = 0;

public NavigationListAdapter(Context context , List<String> navList){
mNavList = navList;
mContext = context;
mlayoutInflater = LayoutInflater.from(mContext);
}
@Override
public int getCount() {
return mNavList.size();
}

@Override
public String getItem(int position) {
return mNavList.get(position);
}

@Override
public long getItemId(int i) {
return 0;
}

@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
View view =convertView;
ViewHolder viewholder;
if(view == null){
view = mlayoutInflater.inflate(R.layout.navigation_list_row,null);
viewholder = new ViewHolder();
viewholder.label = (TextView)view.findViewById(R.id.label);
view.setTag(viewholder);
}else{
viewholder = (ViewHolder)view.getTag();
}
viewholder.label.setText(getItem(i));

if(mSelectedItemIndex != -1 && mSelectedItemIndex == position){
view.setActivated(true);

}
return view;
}

class ViewHolder{
TextView label;
}

public void setmSelectedItemIndex(int selectedItemIndex){
mSelectedItemIndex = selectedItemIndex;
}
}

尝试 1:ListFragment 的 setHighLight() 方法。

尝试:2:

Adapter 类的 getView 中的代码:

/* if(mSelectedItemIndex != -1 && mSelectedItemIndex <= getCount()){
view.setSelected(true);
view.setFocusable(true);

}*/

在我的情况下没有任何效果。

最佳答案

在您的可绘制状态中,您需要使用 state_activated 并在您的 View 中调用方法 setActivated(true) 而不是 view.setSelected(true)

状态可绘制示例:

    <item
android:drawable="@color/color1" android:state_activated="true" android:state_focused="false" android:state_pressed="false"/>

<item
android:drawable="@color/color2" android:state_activated="true" android:state_focused="false" android:state_pressed="true"/>

关于android - ListView 以编程方式突出显示行项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32010266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com