2

我有一个包含自定义列表视图的滑块,android.R.layout_simple_list_item当我将其更改为自己的布局时,它工作正常任何人都告诉我我的代码中有什么问题。谢谢?这是我的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;
}
4

2 回答 2

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;

}
于 2013-06-16T08:11:59.233 回答
0

ListViewXML 在哪里?

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

其次,假设你有一个ListView,你在哪里设置它onItemClickListener?您将需要类似的东西:

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

我会在onViewCreated()方法中这样做。此时, 的存在ListView是有保证的。

于 2013-06-17T00:32:09.960 回答