我在我的项目中设置了listfragment
。但似乎我的片段无法通过我的适配器正确完成。其原因是Context context
在MyListAdapter
。如果我点击纠正它。它变为MenuFragment menuFragment
。但是在更改之后,MyListAdapter
出现错误。所以我纠正它。它变为Context context
。如果我纠正它,它仍然继续下去。它的循环就是这样。使用自定义适配器的ListFragment
注意:我想实现的是带图标的ListFragment。像我之前的其他问题(但不幸的是没有人回答)。
public class MenuFragment extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String [] proMenu ={ "Homies", "Best Nearby", "Coupon" , "Profile" , "History" , "", "Setting" ,
"About" , "Sign Out"};
setListAdapter(new MyListAdapter(this, proMenu));
}
@Override
public void onListItemClick(ListView lv, View v, int position, long id) {
Fragment newContent = null;
switch (position) {
case 0:
newContent = new ColorFragment();
break;
case 1:
Intent intent7 = new Intent();
intent7.setClass(getActivity(), Home.class);
intent7.putExtra("index", position);
startActivity(intent7);
break;
编辑:这是我的布局。它现在完美无瑕。我只需要调整textview和线性布局,这样一个字就不会减半。但我面临着另一个问题。它就像背景图像堆叠起来一样。这是我的布局上的xml。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:cacheColorHint="#00000000"
android:background="@drawable/menu_drawer">
<ImageView
android:id="@+id/row_icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="10dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/row_title"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:padding="10dp"
android:text="Medium Text"
android:textSize="20dp"
android:textAppearance="@android:style/TextAppearance.Medium" />
</LinearLayout>
如果我删除此android:background="@drawable/menu_drawer"
从linear layout
。这将是完美的背景。没有彼此堆放。但是当我在列表中滑动时,背景变得疯狂,它消失了,并显示出一些黑色背景。它像列表视图android:cacheColorHint="#00000000"
的问题一样。我已经在linear layout
中添加了cachecolor
。但它仍然显示出那些黑色背景。就像这样。
我知道什么这个问题的。因为默认背景是黑色的。但我不知道如何解决它。
编辑2:黑色问题解决。
已解决。
首先,你用'机器人:layout_height =“50dp”'该行高度,这是正常的,该文本将被削减,因为它不能适合身高(尤其是当你使用一个20dp大小(用sp代替))。使用'wrap_content'作为行高。其次,'cacheColorHint'属性需要在ListView中使用,而不是行背景(可以在代码中设置)。 – Luksprog 2013-04-11 08:29:56
谢谢。你为我节省了很多。其实我有1个最后的问题。如何在历史和环境之间创造空间。所以这3个列表将会出现在底部(设置,关于和退出)。 – Nicolas 2013-04-11 14:29:55
如果这是某种菜单,那么最好用一个'LinearLayout'替换'ListView',它将包含这些行,这样就可以像想要的那样分开它们。您仍然可以通过计算屏幕所需的确切空间并添加具有该高度的额外空行来使用“ListView”。 – Luksprog 2013-04-11 14:46:22