突出显示列表视图中的多个项目

时间:2015-02-16 11:59:42

标签: android

我真的很难突出我的列表视图中的项目 - 我希望它们在点击时突出显示,并在再次点击它们时不突出显示。有人可以帮助我为我的代码实现这一点,因为我已经在线查看了大量的代码,无法让它工作 - 提前感谢!!这是我的代码

    public class ConversationView extends Activity {
    TextView hello;

    Integer threadId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_conversation_view);

        Intent intent = getIntent();
        String thread = intent.getStringExtra("threadId");

        threadId = Integer.parseInt(thread);

        final ListView convoListView = (ListView) this.findViewById(R.id.conversationListView);

        ArrayList<ConversationItem> convoItems = this.GetItems(threadId);

        final ConversionAdapter convoAdapter = new ConversionAdapter(getApplicationContext(), convoItems);

        convoListView.setAdapter(convoAdapter);

 convoListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        convoListView.setItemChecked(1, true);
   }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_conversation_view, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public ArrayList<ConversationItem> GetItems(Integer threadId)
    {
        ArrayList<ConversationItem> convos = new ArrayList<>();
        convos.add(new ConversationItem(1, 1, "Bob Smith", DateTime.now(), "Lorem ipsum dolor sit amet, "));
        convos.add(new ConversationItem(1, 2, "Bob Smith", DateTime.now(), "Lorem ipsum dolor sit amet,Donec lacinia nunc sed faucibus suscipit. Curabitur."));
        convos.add(new ConversationItem(1, 3, "Bob Smith", DateTime.now(), "Lorem ipsum dolor sit amet, Curabitur."));
        convos.add(new ConversationItem(1, 4, "Bob Smith", DateTime.now(), "Lorem ipsum dolor sit amet, "));

        ArrayList<ConversationItem> returnItems = new ArrayList<>();

        for(int i = 0; i < convos.size(); i++)
        {
            if(convos.get(i).ThreadId.equals(threadId))
            {
                returnItems.add(convos.get(i));
            }
        }

        return returnItems;
    }

//this is my adapter


    public class ConversionAdapter extends ArrayAdapter<ConversationItem>
    {

        private final Context context;
        private final ArrayList<ConversationItem> items;
        private int currentPage = 0;

        public ConversionAdapter(Context context, ArrayList<ConversationItem> convoItems) {
            super(context, 0, convoItems);
            this.context = context;
            this.items = convoItems;
        }



}

1 个答案:

答案 0 :(得分:-1)

我只是在做同样的事情。这对我有用。

@drawable中设置listview,我已将其设置为listview格式,即在布局中放置textViews和checkBoxes以显示在列表视图。

list_view_format.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/list_view_item">

</RelativeLayout>

/res/drawable/list_view_item.xml

<item android:state_pressed="true" >
    <shape>
        <solid android:color="@color/list_item_pressed"/>
    </shape>
</item>

<item android:state_activated="true" >
    <shape>
        <solid android:color="@color/list_item_activated"/>
    </shape>
</item>
<item>
    <shape>
        <solid android:color="@color/list_item_normal" />
    </shape>
</item>

/res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <color name="list_item_normal">#96ffdcb5</color>
    <color name="list_item_activated">#ffaa66</color>
    <color name="list_item_pressed">#ffaa66</color>
</resources>