突出显示ListView中的所选项目不起作用

时间:2017-01-17 02:49:48

标签: android listview

点击它时我有一个按钮,显示带有listview的片段。

stateFragment = new StateFragment();
  FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
  transaction.add(R.id.relativLaout_Sates, stateFragment, "StatFag").commit();

这是onCreateview() StateFragment

上的代码
 listView = (ListView) view.findViewById(R.id.list);
 adapter = new ArrayAdapter<String>(getActivity(), R.layout.row_layout, R.id.row1, items);
 listView.setAdapter(adapter);
 listView.setSelectionFromTop(getpos, getoffset);
 listView.setOnItemClickListener(StateFragment.this);

以及listview的布局代码:

 <ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="@null"
    android:dividerHeight="4dp"
    android:choiceMode="singleChoice"
    />

和行:

 <TextView
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:textSize="@dimen/textSiz_home_page"
    android:id="@+id/row1"
    android:gravity="center"
    android:background="@drawable/bg_txt_add_it"
    android:layout_alignParentEnd="true"
    style="@style/MyTextTheme"/>

,bg_txt_add_it的代码是:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
    <shape>
        <corners android:radius="8dp"/>
        <solid android:color="@color/blue" />
    </shape>
</item>
<item android:drawable="@color/blue2" android:state_activated="true"/>

并在onItemClick中代码为:

 @Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    view.getFocusables(getpos);
    view.setSelected(true);

当我再次打开此ListView时,它会滚动到之前选择的项目但是高亮度选择在两秒后消失,是否有任何建议?

0 个答案:

没有答案