【问题标题】:android custom simplecursoradapter select / highlight a rowandroid自定义simplecursoradapter选择/突出显示一行
【发布时间】:2018-05-01 00:13:46
【问题描述】:

我有一个 ListActivity,然后它管理一个简单的光标适配器来显示文本和图像以及一个月中每一天的分隔符。这个分隔符和输入适配器的信息是通过使用视图绑定器完成的。由于我需要这个应用程序从 API 8 工作,我发现的一些答案似乎不适用于该 API 版本。

到目前为止,我已经尝试使用 OnItemClickListener 来选择行,然后我已经存储了该行的位置,并使用 getView 方法尝试设置背景资源,但它似乎改变的只是颜色的分隔符,不仅是当前选定的行,还有其他的。

OnItemClickListener 已经更改了几次,所以当前代码是我最后一次失败的尝试:

datalist.setOnItemClickListener(new  OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                mC.moveToPosition(arg2); // The Cursor 
                rowchecked = arg2; // Global variable to store the position
                arg1.setBackgroundResource(android.R.color.holo_red_dark); // this is a recent attempt but I knew it would not work 

            Toast.makeText(getApplicationContext(), "Row "+Integer.toString(arg2) + " Clicked", Toast.LENGTH_LONG).show();

            }});

数据列表分配如下:

 ListView datalist = getListView();
 datalist.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);

getView如下:

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.viewdata, mC, fields, viewfields)
        {
            @Override
            public View getView(int position,View convertView, ViewGroup parent) {

                final View row = super.getView(position, convertView, parent);
                if (position == rowchecked) {
                    Toast.makeText(getApplicationContext(), "Detected row selected , row is :"+Integer.toString(rowchecked), Toast.LENGTH_LONG).show();
                 //row.setBackgroundResource(Color.BLUE);
                    row.setBackgroundColor(Color.BLUE);
                mC.moveToPosition(position);
                row.setSelected(true);
        }

                return row;
            }
        };

如果需要 viewbinder 代码,那么我会在请求时发布 - 那里有很多,所以我现在不发布所有内容。

*** 编辑 ****

真的很难让这篇文章的 OnitemLongClick 部分工作 OnitemClick 似乎使用/调用 simplecursoraapter 的 getView 方法,但 OnItemLongClick 侦听器没有。

阅读其他帖子,包括这篇文章 -> how to implement a long click listener on a listview

我已尝试针对代码中的 listview 对象使用 setonlongclick(true),并尝试将其分配给 XML 布局中的 listview,并且我已使用 AdapterView.OnItemLongClick 侦听器创建了 OnItemLongLick 侦听器。

【问题讨论】:

  • 现在想知道我是否需要创建一个自定义适配器来扩展一个简单的光标适配器 - 这是实现这项工作的途径吗?
  • 我的其他问题似乎与我认为的行布局有关,因为它在线性布局下至少有三个表格布局。因此,每个表格布局都有自己的背景颜色以匹配主布局的背景颜色,并且在“触摸”行时需要更改这些颜色
  • 最新的是我可以突出显示该行,但是当我单击另一行时,第一行保持突出显示,然后其他行变得突出显示!

标签: android row highlighting simplecursoradapter


【解决方案1】:

到目前为止,我已经让 onItemLongClickListener 使用适配器的 notifydatasetchange 调用 getView。 My problem is now that when the row is selected, it always highlights the first row in the list.

现在使用 setviewbinder 和 setviewvalue 方法对其进行排序.. 这是代码:

// is the current cursor position the same that has been Long Clicked             
if (cursor.getPosition()==longrowclick1) {  

    // sets up parent view of object  
     myviewp = (View) view.getParent();  
     // checks if parent view is valid (there are a lot here)  
    if (((View)view.getParent()).getId()==R.id.Table1  || ((View)view.getParent()).getId()==R.id.tableRow1 || ((View)view.getParent()).getId()==R.id.tableRow2  || ((View)view.getParent()).getId()==R.id.tableRow3  || ((View)view.getParent()).getId()==R.id.Table2 || ((View)view.getParent()).getId()==R.id.Table3 ) {  

        // If the parent views are valid set the background to dark slate blue  
        myviewp.setBackgroundResource(R.drawable.darkslateblue);  
        // if one of the child views are not the date separator then ..  
        if (view.getId()!=R.id.seper) {  
            // ... set them to the dark slate blue ...  
            view.setBackgroundResource(R.drawable.darkslateblue);  
        } else { // else if the child object is the date separator ..  
            // .. keep it as dark gray  
            view.setBackgroundColor(Color.DKGRAY);  
        }  



    }  




} else {// if the cursor position does not match the Long Click Position then ..  
    // .. get the parent of the current child object ...  
     myviewp = (View) view.getParent();  
     // .. set the background of the parent to black ..  
    myviewp.setBackgroundColor(Color.BLACK);  
    // .. check to make sure that the child object is not the date separator ...  
    if (view.getId()!=R.id.seper) {  
        // .. and then set the child object background to black ..   
        view.setBackgroundColor(Color.BLACK);   
     } else { // .. else if it the child object is the date separator then ..  
         // .. set it to the dark gray background .   
         view.setBackgroundColor(Color.DKGRAY);  
     }  


}  

【讨论】:

    猜你喜欢
    • 2021-02-04
    • 1970-01-01
    • 2014-12-28
    • 2018-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-08
    • 2014-12-05
    相关资源
    最近更新 更多