如何在这种列表上设置onclicklistener

时间:2013-06-16 02:12:12

标签: android android-listview onclicklistener

我有一个滑块,其中包含一个自定义列表视图,当我使用android.R.layout_simple_list_item时它正常工作,当我将其更改为我自己的布局时它看起来更好但是我正在使用的onclick方法停止工作但有错误它只是不应用点击方法任何人都可以告诉我我的代码中的问题是什么。 谢谢? 这是我的java代码

public class MenuFragment extends ListFragment
{
@Override
public void onActivityCreated(Bundle savedInstanceState) 
{
    super.onActivityCreated(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(getActivity(),
        R.layout.burger_list,R.id.textView2, new String[] { " Main menu", " My  schedual", " Location", " Media", " Tickets", "News","Facebook","Instagram","Policy","Share this app"}));
    getListView().setCacheColorHint(0);
}

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

    if(position==0)
    {
        Intent i = new Intent(v.getContext(),ActivityMainMenu.class);
        startActivity(i); 
    }      
    else if(position==1)
     {
        Intent i = new Intent(v.getContext(),ActivityMySchedual.class);
        startActivity(i); 
     }

    else if(position==2)
    {
        Intent i = new Intent(v.getContext(),ActivityMySchedual.class);
       startActivity(i); 
    }
    else if(position==3)
    {
        Intent i = new Intent(v.getContext(),ActivityMedia.class);
       startActivity(i); 
    }

}
}

这是xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#556B2F" >

<ScrollView
    android:id="@+id/scrolololo"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true" >

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:background="#556B2F" >

        <TableRow
            android:id="@+id/tablerow"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clickable="true"
            android:gravity="center_vertical"
            android:paddingBottom="7dp"
            android:paddingTop="7dp" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="320dp"
                android:layout_height="40dp"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#FFFFFF" />
        </TableRow>

        <View
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:background="@drawable/main_list_sep" />
    </TableLayout>
</ScrollView>
</RelativeLayout>

这个类是我的滑块。我在MenuFragment中更改的所有内容都会影响它。我被卡住了!

public class ActivityMenu extends FragmentActivity{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    mSlideoutHelper = new SlideoutHelper(this);
    mSlideoutHelper.activate();
     getSupportFragmentManager().beginTransaction().add(com.korovyansk.android.slideout.R.id.slideout_placeholder, new MenuFragment(), "menu").commit();
    mSlideoutHelper.open();
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_BACK){
        mSlideoutHelper.close();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}


public SlideoutHelper getSlideoutHelper(){
    return mSlideoutHelper;
}

private SlideoutHelper mSlideoutHelper;
}

2 个答案:

答案 0 :(得分:1)

我认为这种情况适合您,因为在您的布局中,有些东西会从您的点击中窃取焦点 尝试将此行放在您拥有的行布局上,以及其中的所有项目,在各自的xml文件中

 android:descendantFocusability="blocksDescendants"

编辑:

public class MenuFragment extends ListFragment
{
@Override
public void onActivityCreated(Bundle savedInstanceState) 
{
    super.onActivityCreated(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(getActivity(),
        R.layout.burger_list,R.id.textView2, new String[] { " Main menu", " My  schedual", " Location", " Media", " Tickets", "News","Facebook","Instagram","Policy","Share this app"}));
    getListView().setCacheColorHint(0);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) 
{
    super.onListItemClick(l, v, position, id); //<-- remove this line, you are supposed to send //it if you don't handle the event, not if you do
    ((ActivityMenu)getActivity()).getSlideoutHelper().close();

    if(position==0)
    {
        Intent i = new Intent(v.getContext(),ActivityMainMenu.class);
        startActivity(i); 
    }      
    else if(position==1)
     {
        Intent i = new Intent(v.getContext(),ActivityMySchedual.class);
        startActivity(i); 
     }

    else if(position==2)
    {
        Intent i = new Intent(v.getContext(),ActivityMySchedual.class);
       startActivity(i); 
    }
    else if(position==3)
    {
        Intent i = new Intent(v.getContext(),ActivityMedia.class);
       startActivity(i); 
    }

}
}




    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:descendantFocusability="blocksDescendants" //<-- put this line here, also put it in the    XML //where your listview resides
android:background="#556B2F" >

<ScrollView
    android:id="@+id/scrolololo"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
   android:descendantFocusability="blocksDescendants" //<-- put this line here
    android:layout_alignParentRight="true" >

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:descendantFocusability="blocksDescendants" //<-- put this line here
        android:background="#556B2F" >

        <TableRow
            android:id="@+id/tablerow"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clickable="true"
            android:gravity="center_vertical"
            android:paddingBottom="7dp"
            android:paddingTop="7dp" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="320dp"
                android:layout_height="40dp"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#FFFFFF" />
        </TableRow>

        <View
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:background="@drawable/main_list_sep" />
    </TableLayout>
  </ScrollView>

 </RelativeLayout>






    public class ActivityMenu extends FragmentActivity{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    mSlideoutHelper = new SlideoutHelper(this);
    mSlideoutHelper.activate();
     getSupportFragmentManager().beginTransaction().add(com.korovyansk.android.slideout.R.id.slideout_placeholder, new MenuFragment(), "menu").commit();
    mSlideoutHelper.open();
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_BACK){
        mSlideoutHelper.close();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}


public SlideoutHelper getSlideoutHelper(){
    return mSlideoutHelper;
}

private SlideoutHelper mSlideoutHelper;

}

答案 1 :(得分:0)

XML中的ListView在哪里?

在Android文档中,因为您使用的是ListFragment,所以您可以在XML中使用ID ListView声明@android:id/list。它是规范列表标识符。

其次,假设您有ListView,您在哪里设置onItemClickListener?你需要这样的东西:

getListView().setOnItemClickListener(new OnItemClickListener() {...});

我在onViewCreated()方法中执行此操作。此时ListView的存在得到保证。