簡體   English   中英

在Fragment內顯示帶有自定義列表適配器的listView

[英]Show listView with custom list adapter inside Fragment

我面臨的問題是在帶有Tabbed Activity Fragment顯示自定義listView 這是我的java和xml文件

Section2Fragment.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

/**
 * A placeholder fragment containing a simple view.
 */
public class Section2Fragment extends Fragment {

   public Section2Fragment() {
   }

   ListView list;

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

       View view = inflater.inflate(R.layout.fragment_section2, container, false);

       Log.d(getFragmentManager().toString(), "Fragment Section 2");
       final String[] itemname = {
            "Safari",
            "Camera",
            "Global",
            "FireFox",
            "UC Browser",
            "Android Folder",
            "VLC Player",
            "Cold War"
       };

       Integer[] imgid = {
            R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher,
       };

       CustomListAdapter adapter = new CustomListAdapter(getActivity(), itemname, imgid);
       list = (ListView) view.findViewById(R.id.section2listView);
       list.setAdapter(adapter);

       list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

           @Override
           public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
               // TODO Auto-generated method stub
               String Slecteditem = itemname[+position];
               Toast.makeText(getActivity(), Slecteditem, Toast.LENGTH_SHORT).show();

           }
       });
       return inflater.inflate(R.layout.fragment_section2, container, false);

    }
}

CustomListAdapter.java

import android.app.Activity;
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 CustomListAdapter extends ArrayAdapter<String> {

    private final Activity context;
    private final String[] itemname;
    private final Integer[] imgid;

    public CustomListAdapter(Activity context, String[] itemname, Integer[] imgid) {
        super(context, R.layout.listview_item, itemname);
        // TODO Auto-generated constructor stub

        this.context = context;
        this.itemname = itemname;
        this.imgid = imgid;
    }


    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.listview_item, null, true);

        TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
        TextView extratxt = (TextView) rowView.findViewById(R.id.textView1);

        txtTitle.setText(itemname[position]);
        imageView.setImageResource(imgid[position]);
        extratxt.setText("Description " + itemname[position]);
        return rowView;

    }
}

fragment_section2.xml

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:background="#ff8720"
        android:id="@+id/section2container">
        <ListView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/section2listView">

        </ListView>
    </LinearLayout>

</RelativeLayout>

listview_item.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="match_parent"
    android:background="#d7d7d7"
    android:orientation="horizontal">
    <ImageView
        android:id="@+id/icon"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:padding="5dp" />

    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:padding="2dp"
            android:textColor="#33CC33" />
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#2c7bff"
            android:text="TextView"
            android:layout_marginLeft="10dp"/>
    </LinearLayout>
</LinearLayout>

我的問題是為什么列表項沒有出現在屏幕上? 解決該問題該怎么辦?

問題出在您的onCreateView中。 您使用以下方法為布局充氣

View view = inflater.inflate(R.layout.fragment_section2, container, false);

獲取對listview的引用,而不是返回這種膨脹的布局,而是返回一個新的膨脹的布局。

更換您的

return inflater.inflate(R.layout.fragment_section2, container, false);

return view;

在您的onCreateView()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM