Listview选择器由于某种原因不起作用

时间:2016-05-04 15:17:30

标签: android android-layout listview

我将这些行放在/res/drawable/listview_main.xml布局中:template<typename D, typename B> class IsDerivedFromHelper { class No { }; class Yes { No no[3]; }; static Yes Test( B* ); static No Test( ... ); public: enum { Is = sizeof(Test(static_cast<D*>(0))) == sizeof(Yes) }; }; template <class C, class P> bool IsDerivedFrom() { return IsDerivedFromHelper<C, P>::Is; } 并创建一个选择器(葡萄牙语中的seletor)。但由于某种原因,它不起作用,然后我尝试以编程方式调用Listview选择器。再次,当我点击Listview中的项目时,没有显示选择器(它仍然是透明的,没有颜色)。我不知道为什么会这样。另外,我放android:listSelector="@drawable/seletor2",如果我的ListView上有背景Drawable。我需要一些帮助。

listview_main.xml

listview.setDrawSelectorOnTop(true);

seletor2

<ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/shape2"  
            android:layout_weight="1"


            android:paddingBottom="7dp"
            android:layout_marginTop="3dp"
        android:layout_marginBottom="3dp"
            android:layout_marginLeft="0dp"
            android:layout_marginRight="0dp"
            android:divider="@color/red"
            android:listSelector="@drawable/seletor2"
            android:drawSelectorOnTop="true" 
                android:dividerHeight="2px"
         />

活动

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/red" />
<item android:drawable="@android:color/transparent" />
</selector>

ListViewAdapter

public class MainActivity extends Activity {
    // Declare Variables
    ListView listview;
    ListViewAdapter adapter;
    ProgressDialog mProgressDialog;
    static String DEFINITION = "definition";
    static String WORD = "word";
    static String FLAG = "flag";
    EditText editsearch;
    private List<WorldPopulation> worldpopulationlist = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // Get the view from listview_main.xml
        setContentView(R.layout.listview_main);
        // Execute DownloadXML AsyncTask
        new DownloadXML().execute();
    }

    // DownloadXML AsyncTask
    private class DownloadXML extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Create a progressdialog
            mProgressDialog = new ProgressDialog(MainActivity.this);
            // Set progressdialog title
            mProgressDialog.setIcon(R.drawable.favicon);
            mProgressDialog.setTitle("Search Parse");
            // Set progressdialog message
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(false);
            // Show progressdialog
            mProgressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            // Create the array
            worldpopulationlist = new ArrayList<WorldPopulation>();
            XMLParser parser = new XMLParser();
            // Retrive nodes from the given website URL in XMLParser.class
            String xml = parser
                    .getXmlFromUrl("http://ecxmlparser.esy.es/br.xml");
            // Retrive DOM element
            Document doc = parser.getDomElement(xml);

            try {
                // Locate the NodeList name
                NodeList nl = doc.getElementsByTagName("vocabulary");
                for (int i = 0; i < nl.getLength(); i++) {
                    Element e = (Element) nl.item(i);
                    WorldPopulation map = new WorldPopulation();
                    map.setDefinition(parser.getValue(e, DEFINITION));
                    map.setWord(parser.getValue(e, WORD));
                    map.setFlag(parser.getValue(e, FLAG));

                    worldpopulationlist.add(map);
                }
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {
            // Locate the listview in listview_main.xml
            listview = (ListView) findViewById(R.id.listview);

            // Pass the results into ListViewAdapter.java
            Collections.sort(worldpopulationlist, new Comparator<WorldPopulation>() {
                @Override
                public int compare(WorldPopulation lhs, WorldPopulation rhs) {
                    return lhs.getWord().compareToIgnoreCase(rhs.getWord());
                }
            });

            adapter = new ListViewAdapter(MainActivity.this,
                    worldpopulationlist);
            // Binds the Adapter to the ListView
            listview.setAdapter(adapter);

            listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Intent i = new Intent(MainActivity.this, SingleItemView.class);


                WorldPopulation voc = (WorldPopulation) adapter.getItem(position);



                i.putExtra("definition", voc.getDefinition());
                // Pass word
                i.putExtra("word", voc.getWord());


                i.putExtra("flag", voc.getFlag());

                i.putExtra("position", position);
                // Open SingleItemView.java Activity
                startActivity(i);

            }

        });



            // Close the progressdialog
            mProgressDialog.dismiss();
            // Locate the EditText in listview_main.xml
            editsearch = (EditText) findViewById(R.id.search);

            // Capture Text in EditText
            editsearch.addTextChangedListener(new TextWatcher() {

                @Override
                public void afterTextChanged(Editable arg0) {
                    // TODO Auto-generated method stub
                    String text = editsearch.getText().toString()
                            .toLowerCase(Locale.getDefault());
                    adapter.filter(text);
                }

                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1,
                        int arg2, int arg3) {
                    // TODO Auto-generated method stub
                }

                @Override
                public void onTextChanged(CharSequence arg0, int arg1,
                        int arg2, int arg3) {
                    // TODO Auto-generated method stub
                }
            });
        }
    }
}

2 个答案:

答案 0 :(得分:0)

只需将它用于选择器

 <item android:state_pressed="true" android:drawable="@color/red" />
<item android:drawable="@android:color/transparent" />

因为您不需要其他状态的项目。并将drawSelector设置为top true。 Listview上的setOnItemClickListener()。确保您在适配器中使用的布局为其父布局充气不应该是可点击的。否则它将拦截OnItemClick列表,你将看不到从列表视图中获取OnItemClick调用..

答案 1 :(得分:0)

我有一个解决方案。我把这行放在ListViewAdapter:view.setBackgroundResource(R.drawable.seletor2);中,现在它工作正常!