【问题标题】:ListFragment with Custom Adapter带有自定义适配器的 ListFragment
【发布时间】:2013-04-11 04:07:25
【问题描述】:

我在我的项目中设置了listfragment。但似乎我的片段无法用我的适配器正确处理。这是因为MyListAdapter 中的Context context。如果我点击纠正它。它变为MenuFragment menuFragment。但是在更改之后,MyListAdapter 出错了。所以我纠正它。它变为Context context。如果我再次纠正它,它仍然会继续下去。它像那样循环。

注意:我想要实现的是带有图标的 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>

如果我从linear layout 中删除这个android:background="@drawable/menu_drawer"。这将是完美的背景。不互相堆积。但是当我在列表中滑动时,背景变得疯狂,它消失了,并在其中显示了一些黑色背景。就像 listview android:cacheColorHint="#00000000" 的问题一样。我已经在linear layout 中添加了cachecolor。但它仍然显示那些黑色背景。就像这样。

我知道问题出在哪里。这是因为默认背景是黑色的。但我不知道如何解决它。

EDIT2:黑色问题已解决。

已解决。

【问题讨论】:

  • 首先,您使用android:layout_height="50dp" 作为行高,因此文本将被剪切是正常的,因为它无法适应该高度(尤其是当您使用 20dp 大小时(使用 sp 代替))。使用wrap_content 作为行高。其次,cacheColorHint 属性需要用于ListView 而不是行背景(您可以在代码中设置)。
  • 谢谢。你救了我很多。实际上我有 1 个最后的问题。如何在历史和背景之间留出空间。所以 3 列表将在底部(设置、关于和退出)。
  • 如果那是某种菜单,那么您最好将ListView 替换为包含行的LinearLayout,这样您就可以根据需要将它们分开。您仍然可以使用ListView 来计算屏幕所需的确切空间并添加一个具有该高度的额外空行。
  • 谢谢。我更新了问题:D 实际上是它的菜单。 jfeinstein10 的幻灯片菜单
  • 我说过要插入一个空行,您可以为其计算适当的高度。您真的应该就此提出一个新问题。

标签: android android-listview slider android-listfragment android-adapter


【解决方案1】:

您没有将有效的Context 传递给您的适配器,Fragment 不是Context。例如,您需要使用Activity 作为Context

setListAdapter(new MyListAdapter(getActivity(), proMenu));

我希望你也在适配器中实现getCount() 方法,否则无论你有多少元素,你都不会在ListView 中看到任何内容。

【讨论】:

  • 非常感谢你 非常感谢。终于在一周后解决了。仅供参考 = 我没有实现 getCount() 并且它仍然可以正常工作。但是有一个问题。我的文字看起来不太适合列表。让我们说什么时候菜单“附近”。单词“y”的底部没有填写列表。它只是一半。它就像“你”。
  • @Nicolas 底部的文字被截断了?你能发布你的布局文件吗?
【解决方案2】:
Following is the method to create listFragement list view:
HealthAdvice.java

package com.example.babs;


import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class HealthAdvice extends ListFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.health_advice, container, false);

        return rootView;

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        HealthAdviceArray health_data[] = new HealthAdviceArray[]
                {
                new HealthAdviceArray(R.drawable.ic_launcher, "Cloudy"),
                new HealthAdviceArray(R.drawable.ic_launcher, "Showers"),
                new HealthAdviceArray(R.drawable.ic_launcher, "Snow"),
                new HealthAdviceArray(R.drawable.ic_launcher, "Storm"),
                new HealthAdviceArray(R.drawable.ic_launcher, "Sunny")
                };

        HealthAdviceAdapter adapter = new HealthAdviceAdapter(getActivity(), 
                R.layout.health_advice_item_row, health_data);

        /** Setting the array adapter to the list view */
        setListAdapter(adapter);

    }

}// end main class HealthAdvice

Step 2:

HealthAdviceAdapter.java
package com.example.babs;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class HealthAdviceAdapter extends ArrayAdapter<HealthAdviceArray>{

    Context context; 
    int layoutResourceId;    
    HealthAdviceArray data[] = null;

    public HealthAdviceAdapter(Context context, int layoutResourceId, HealthAdviceArray[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        HealthAdviceArrayHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new HealthAdviceArrayHolder();
            holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
            holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);

            row.setTag(holder);
        }
        else
        {
            holder = (HealthAdviceArrayHolder)row.getTag();
        }

        HealthAdviceArray weather = data[position];
        holder.txtTitle.setText(weather.title);
        holder.imgIcon.setImageResource(weather.icon);

        return row;
    }

    static class HealthAdviceArrayHolder
    {
        ImageView imgIcon;
        TextView txtTitle;
    }
}

Step 3:

HealthAdviceArray.java

package com.example.babs;


public class HealthAdviceArray {

    public int icon;
    public String title;

    // we are over loading here
    public HealthAdviceArray(){
        super();
    }

    public HealthAdviceArray(int icon, String title) {
        super();
        this.icon = icon;
        this.title = title;
    }

}

step 4: 

Health Advice health_advice.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

step 5:

Health Advice items: health_advice_item_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:padding="10dp" >

    <ImageView
        android:id="@+id/imgIcon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginBottom="5dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="5dp"
        android:contentDescription="@string/image_view"
        android:gravity="center_vertical" />

    <TextView
        android:id="@+id/txtTitle"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:gravity="center_vertical"
        android:textColor="#000000"
        android:textSize="22sp"
        android:textStyle="bold" />

</LinearLayout>

【讨论】:

    【解决方案3】:

    在创建自定义适配器时,不要将“上下文”对象作为引用传递,只需将 Activity 对象传递给自定义适配器即可。

    public class HealthAdvice extends ListFragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
            View rootView = inflater.inflate(R.layout.health_advice, container, false);
    
            return rootView;
    
        }
    
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
    
            HealthAdviceArray health_data[] = new HealthAdviceArray[]
                    {
                    new HealthAdviceArray(R.drawable.ic_launcher, "Cloudy"),
                    new HealthAdviceArray(R.drawable.ic_launcher, "Showers"),
                    new HealthAdviceArray(R.drawable.ic_launcher, "Snow"),
                    new HealthAdviceArray(R.drawable.ic_launcher, "Storm"),
                    new HealthAdviceArray(R.drawable.ic_launcher, "Sunny")
                    };
    
            HealthAdviceAdapter adapter = new HealthAdviceAdapter(getActivity(), 
                    R.layout.health_advice_item_row, health_data);
    
            /** Setting the array adapter to the list view */
            setListAdapter(adapter);
    
        }
    
    }// end main class HealthAdvice
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-21
      • 2023-03-17
      • 2013-10-07
      • 2023-03-19
      相关资源
      最近更新 更多