【问题标题】:Display two Listviews in fragment one after another在fragment中依次显示两个Listview
【发布时间】:2018-03-17 10:13:32
【问题描述】:

我使用下面的代码用 ListView 填充我的片段,效果很好。 现在我需要分离 Listview 并在其间添加 Textview。所以,因此我现在需要显示两个 ListView,Listview A 和 ListView B,每个单独的数据,即 ListView A 将显示数据 A,而 ListView B 将显示数据 B。

我想仅在 Listview A 结束时才启动 Listview B,而不是像下图那样占用一半长度

并为类似于A的Listview B单独应用onClicklistener。

我该怎么做??

提前致谢。

我的 Fragment.java 文件是,

package com.nepalpolice.cdp;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * Created by Sagar on 2017/09/23.
 */


public class club extends Fragment  {
    // Array of strings storing country names
    String[] countries = new String[]{
            "Royal Physicist of CDP",
            "MSC Physics of CDP",
            "Msc PHYSICS 2073B.S BATCH ",

    };

    // Array of integers points to images stored in /res/drawable-ldpi/
    int[] flags = new int[]{
            R.drawable.rpl,
            R.drawable.cdpl,
            R.drawable.mscl,

    };

    // Array of strings to store currencies
    String[] currency = new String[]{
            "This page is Dedicated with Informative trolls and memes.",
            "This Page is Dedicated for information",
            "Join this page for news and Notices",

    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_club, container, false);
// 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 < 3; i++) {
            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(), aList, R.layout.listview_layout, from, to);
        // Getting a reference to listview of main.xml layout file
        ListView listView = (ListView) view.findViewById(R.id.listview);
        // Setting the adapter to the listView
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(itemClickListener);
        return view;
    }

// Setting the adapter to the listView


    // Item Click Listener for the listview
    OnItemClickListener itemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View container, int position, long id) {
            // Getting the Container Layout of the ListView
            if(position == 0/*or any other position*/){
                Fragment fragment = new royal();
                FragmentManager fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.fragment_frame, fragment).addToBackStack(null).commit();
            }
            else if(position == 1){
                Fragment fragment = new cdp();
                FragmentManager fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.fragment_frame, fragment).addToBackStack(null).commit();

            } // etc...

            else if(position == 2){
                Fragment fragment = new msc();
                FragmentManager fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.fragment_frame, fragment).addToBackStack(null).commit();

            } // etc...
        }
    };
}

我的 listview_layout.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:orientation="horizontal" >

    <ImageView
        android:id="@+id/flag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/hello"
        android:paddingTop="10dp"
        android:paddingRight="10dp"
        android:paddingBottom="10dp" />

    <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/mero"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10px"
            android:textAppearance="?android:attr/textAppearanceSmall"
            />

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


    </LinearLayout>
</LinearLayout>

而我的 fragment_club.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" >

    <TextView
        android:id="@+id/textview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

【问题讨论】:

  • 最好使用recyclerviewer并实现滚动结束监听器并在recyclerviewer到达末尾时附加列表项
  • @Dinesh 你能帮我怎么做吗??
  • 是的,我会给出一些指导让我检查一下
  • 您可以像这样使用回收站视图并实现多视图类型。 stackoverflow.com/a/26245463/4198372

标签: java android android-layout listview


【解决方案1】:

我不会推荐你所做的。最好使用 recyclerview,它比普通的 listview 更强大。首先,您必须使用标题构建 recyclerview,请参阅以下帖子:https://inducesmile.com/android/add-header-to-android-recyclerview/

根据您的要求,您希望在用户滚动到达末端 recyclerview 时加载下一组数据,因此您必须添加滚动侦听器,以告知您是否到达 recyclerview 末端。你可以参考下面的帖子来实现这个:

https://medium.com/@ayhamorfali/android-detect-when-the-recyclerview-reaches-the-bottom-43f810430e1e

一旦您知道何时到达 recyclerview 的末尾,您就必须将新数据添加到数据集中并刷新 recyclerview。

【讨论】:

  • 感谢您的指导和建议。尽管我已经在 Playstore 上发布了 5 个应用程序,但我对 Android 完全陌生,但我仍然开始理解编码。所以你现在建议我的远不是我能做的.....这很容易,我可以理解和学习,只要你能给我提供解决方案,我在谷歌上搜索,但找不到任何同一主题的教程...因此对未来的教程也将更受益
  • @BlueYeti81 很高兴分享我从经验中学到的东西。 :)
猜你喜欢
  • 2016-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-06
  • 2023-04-03
  • 2016-04-05
相关资源
最近更新 更多