ListFragment Listview - 如何自动将第一个listview项设置为选定状态?

时间:2016-10-16 13:30:56

标签: android listview android-fragments

我有一个ListView的ListFragment。我想要做的是 - 当创建ListFragment时,我想要自动选择(突出显示)它的列表视图中的第一项。为突出显示listview行,我使用自定义列表选择器。我尝试将列表的第一项设置为选定状态,因此列表选择器也可以突出显示它。但是我的代码没有任何效果。也许有人可以帮我解决这个任务?我做错了什么?

更新:这是我的ListFragment代码:

public class ObjectListFragment extends ListFragment {

private DatabaseHandler databaseHandler;
private ArrayList<FFObject> ffObjects = new ArrayList<>();
private FFObjectsAdapter ffObjectAdapter;
private ArrayList<FFObject> arrayList;
private FFObject ffObject;
private long objectId;

static interface ObjectListListener{
    void itemClicked(long id);        
}

private ObjectListListener listener;

public ObjectListFragment() {
    // Required empty public constructor
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    this.listener = (ObjectListListener) context;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    refreshData();        
    return inflater.inflate(R.layout.fragment_object_list, null);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);                      
}

@Override
public void onResume() {
    super.onResume();

    ListView listView = getListView();
    listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    listView.setItemChecked(2,true);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);        

    arrayList = this.ffObjects;        
    ffObject = arrayList.get(position);
    long itemId = ffObject.getObjectID();

    Log.d("ObjectListFragment","Clicked on item with id = "+itemId);
    if (listener != null){
        listener.itemClicked(itemId);
    }        
}

public long getObjectId() {

    arrayList = this.ffObjects;
    ffObject = arrayList.get(0);
    objectId = ffObject.getObjectID();

    return objectId;
}

private void refreshData() {
    ffObjects.clear();
    databaseHandler = new DatabaseHandler(this.getActivity().getApplicationContext());
    ArrayList<FFObject> ffobjectsFromDB = databaseHandler.getObjectsList();
    for (int i = 0; i < ffobjectsFromDB.size(); i++) {
        String title = ffobjectsFromDB.get(i).getTitle();
        String dateText = ffobjectsFromDB.get(i).getRecordDate();
        long objectid = ffobjectsFromDB.get(i).getObjectID();

        FFObject ffobject = new FFObject();
        ffobject.setTitle(title);
        ffobject.setRecordDate(dateText);
        ffobject.setObjectID(objectid);
        ffObjects.add(ffobject);
    }
    databaseHandler.close();
    //setup adapter
    ffObjectAdapter = new FFObjectsAdapter(this.getActivity(), R.layout.object_row, ffObjects);
    setListAdapter(ffObjectAdapter);
    ffObjectAdapter.notifyDataSetChanged();
}

public class FFObjectsAdapter extends ArrayAdapter<FFObject>{

    Context context;
    int layoutResource;
    FFObject ffObject;
    ArrayList<FFObject> ffObjectArrayList = new ArrayList<>();

    public  FFObjectsAdapter (Context con, int resource, ArrayList<FFObject> data){
        super(con,resource,data);
        context = con;
        layoutResource = resource;
        ffObjectArrayList = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return ffObjectArrayList.size();
    }

    @Override
    public FFObject getItem(int position) {
        return ffObjectArrayList.get(position);
    }

    @Override
    public int getPosition(FFObject item) {
        return super.getPosition(item);
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View row = convertView;
        ViewHolder holder = null;

        if (row == null || row.getTag()==null){
            LayoutInflater inflater = LayoutInflater.from(context);
            row = inflater.inflate(layoutResource, null);
            holder = new ViewHolder();
            holder.ffTitle = (TextView) row.findViewById(R.id.objectName);
            holder.ffDate = (TextView) row.findViewById(R.id.dateText);

            row.setTag(holder);

        } else {
            holder = (ViewHolder) row.getTag();
        }

        holder.ffObject = getItem(position);
        holder.ffTitle.setText(holder.ffObject.getTitle());
        holder.ffDate.setText(holder.ffObject.getRecordDate());
        holder.ffId = holder.ffObject.getObjectID();

        final  ViewHolder finalHolder = holder;           

        return row;
    }

    class  ViewHolder{
        FFObject ffObject;
        TextView ffTitle;
        long ffId;
        TextView ffDate;
    }
}

@Override
public void onDetach() {
    super.onDetach();
    this.listener= null;
}
}

我的fragment_object_list.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:orientation="vertical">

<ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:choiceMode="singleChoice"
        android:listSelector="@drawable/list_selector">
</ListView>

<TextView
        android:id="@id/android:empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="@string/no_objects_in_application" >
</TextView>

</LinearLayout>

我的object_row.xml代码

<?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="match_parent"
            android:orientation="vertical"
            android:padding="12dp">

<ImageView
        android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:src="@android:drawable/ic_menu_agenda"
/>

<TextView
        android:id="@+id/objectName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="4dp"
        android:layout_toRightOf="@id/thumbnail"
        android:text="Object title"
        android:textSize="13sp"
        android:textStyle="bold"
/>

<TextView
        android:id="@+id/dateText"
        android:text="date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/thumbnail"
        android:layout_below="@id/objectName"
        android:layout_marginLeft="4dp"
        android:textSize="12dp"
        android:textStyle="italic"

/>

</RelativeLayout>

我的list_selector.xml代码

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- normal state. -->
    <item android:state_selected="false" android:state_pressed="false"  android:drawable="@drawable/list_selector_selected" />
    <!-- pressed state. -->
    <item android:state_pressed="true" android:drawable="@drawable/list_selector_selected" />
    <!-- selected state. -->
    <item android:state_selected="true" android:state_pressed="false"  android:drawable="@drawable/list_selector_selected" />

    <item android:state_activated="true" android:drawable="@drawable/list_selector_selected" />
</selector>

我的list_selector_selected.xml代码

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"     android:shape="rectangle">
    <gradient
            android:endColor="#f7ddb8"
            android:startColor="#f5c98c"
            android:angle="90">
    </gradient>

</shape>

我不在列表中使用单选按钮或复选框。所以我不认为我的问题是重复的topic。我成功使用列表选择器在单击时更改列表行的背景颜色。我的问题是:在列表片段活动开始后,如何将列表的TOP(第一行)设置为选定状态?我想自动完成,而不是用户点击事件。在这里的示例代码中,我使用position = 2.我知道这不是第一行,但此刻并不重要。无论如何,代码根本不起作用。行选择仅在点击事件中正确。

0 个答案:

没有答案