Android 中的 Listfragment 按钮显示

Listfragment button display in Android

我正在开发 Android 应用程序,我在其中使用了列表片段。我在布局的顶部添加了一个按钮。我的问题是它在每个列表选项中重复出现。我只想让它在印度选项列表的顶部只显示一次。

我的快照代码如下:

public class CountryList extends ListFragment {


    Button btn1;
    // Array of strings storing country names
    String[] countries = new String[] {
            "India",
            "Pakistan",
            "Sri Lanka",
            "China",
            "Bangladesh",
            "Nepal",
            "Afghanistan",
            "North Korea",
            "South Korea",
            "Japan"
    };    

    // Array of integers points to images stored in /res/drawable/
    int[] flags = new int[]{
            R.drawable.india,
            R.drawable.pakistan,
            R.drawable.srilanka,
            R.drawable.china,
            R.drawable.bangladesh,
            R.drawable.nepal,
            R.drawable.afghanistan,
            R.drawable.nkorea,
            R.drawable.skorea,
            R.drawable.japan    
    };

    // Array of strings to store currencies
    String[] currency = new String[]{
        "Indian Rupee",
        "Pakistani Rupee",
        "Sri Lankan Rupee",
        "Renminbi",
        "Bangladeshi Taka",
        "Nepalese Rupee",
        "Afghani",
        "North Korean Won",
        "South Korean Won",
        "Japanese Yen"
    };


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


        // Each row in the list stores country name, currency and flag
        List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();        

        for(int i=0;i<10;i++){
            if(countries[i]=="India"){

            }
            HashMap<String, String> hm = new HashMap<String,String>();
            hm.put("txt", "Country : " + countries[i]);
            hm.put("cur","Currency : " + currency[i]);
            hm.put("flag", Integer.toString(flags[i]) );            
            aList.add(hm);        
        }

        // Keys used in Hashmap
        String[] from = { "flag","txt","cur" };

        // Ids of views in listview_layout
        int[] to = { R.id.flag,R.id.txt,R.id.cur};        

        // Instantiating an adapter to store each items
        // R.layout.listview_layout defines the layout of each item
        SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.listview_layout, from, to);       

        setListAdapter(adapter);

        return super.onCreateView(inflater, container, savedInstanceState);     
    }

}

我的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="vertical"    
     >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Button" />

    <ImageView 
            android:id="@+id/flag"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:contentDescription="HELLO WORLD"
            android:layout_gravity="center"
            />

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

            <TextView 
                android:id="@+id/txt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="15dp" />          

            <TextView 
                android:id="@+id/cur"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="10dp" />

        </LinearLayout>

</LinearLayout>

而不是在 row_layout 中添加按钮.. 创建新的 Xml 仅使用该按钮.. 将此布局添加为 listView 的 headerView。 这将在列表视图的顶部添加按钮。 如果出现问题,请告诉我! 干杯!

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


        // Each row in the list stores country name, currency and flag
        List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();        

        for(int i=0;i<10;i++){
            if(countries[i]=="India"){

            }
            HashMap<String, String> hm = new HashMap<String,String>();
            hm.put("txt", "Country : " + countries[i]);
            hm.put("cur","Currency : " + currency[i]);
            hm.put("flag", Integer.toString(flags[i]) );            
            aList.add(hm);        
        }

        // Keys used in Hashmap
        String[] from = { "flag","txt","cur" };

        // Ids of views in listview_layout
        int[] to = { R.id.flag,R.id.txt,R.id.cur};        

        // Instantiating an adapter to store each items
        // R.layout.listview_layout defines the layout of each item
        SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.listview_layout, from, to);       
View headerView;
 headerView = inflater.inflate(R.layout.list_header, null,false);
 getListView().addHeaderView(mHeaderView);
        setListAdapter(adapter);

        return super.onCreateView(inflater, container, savedInstanceState);     
    }

我认为您已经为您的列表制作了自定义布局。

您正在使用 SimpleAdapter。但是根据您的要求,您需要制作一个自定义适配器,您可以将其实现为 SimpleAdapterBaseAdapter。在那个 class 中,您会找到一个 getView() 方法,您可以在其中扩充自定义视图并检查以下条件。

  if(countries[position].eqauls("India")) {
      yourButton.setVisibility(View.VISIBLE);
  }else{
      yourButton.setVisibility(View.GONE);
  }