如何使列表视图出现在片段中?

问题描述 投票:0回答:2

我正在尝试让数组提供列表视图,并且使用了Android Studio随附的一些示例代码。为什么我的列表视图没有出现在片段中?

我正在将Vogella Android List View中的代码用于列表视图适配器。我不明白自己在做错什么导致列表视图不出现。它应该非常简单。

package com.example.bento.ui.home;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.example.bento.R;

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

public class HomeFragment extends Fragment {

    private HomeViewModel homeViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        homeViewModel =
                ViewModelProviders.of(this).get(HomeViewModel.class);
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        final TextView textView = root.findViewById(R.id.text_home);
        homeViewModel.getText().observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });

        final ListView listview = (ListView) root.findViewById(R.id.listview);
        String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
                "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
                "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
                "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
                "Android", "iPhone", "WindowsMobile" };

        final ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < values.length; ++i) {
            list.add(values[i]);
        }
        final StableArrayAdapter adapter = new StableArrayAdapter(getContext(),
                android.R.layout.simple_list_item_1, list);
        listview.setAdapter(adapter);

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onItemClick(AdapterView<?> parent, final View view,
                                    int position, long id) {
                final String item = (String) parent.getItemAtPosition(position);
                view.animate().setDuration(2000).alpha(0)
                        .withEndAction(new Runnable() {
                            @Override
                            public void run() {
                                list.remove(item);
                                adapter.notifyDataSetChanged();
                                view.setAlpha(1);
                            }
                        });
            }

        });

        return root;
    }

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



    }

    private class StableArrayAdapter extends ArrayAdapter<String> {

        HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

        public StableArrayAdapter(Context context, int textViewResourceId,
                                  List<String> objects) {
            super(context, textViewResourceId, objects);
            for (int i = 0; i < objects.size(); ++i) {
                mIdMap.put(objects.get(i), i);
            }
        }

        @Override
        public long getItemId(int position) {
            String item = getItem(position);
            return mIdMap.get(item);
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

    }
}

这是我的布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/listview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <TextView
        android:id="@+id/text_home"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:textAlignment="center"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

在我的家庭片段中,没有列表视图出现。

android android-fragments android-listview
2个回答
0
投票

将布局中的ListView标记更改为此:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listview"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/text_home"/>

通过将宽度和高度设置为0dp并提供所有四个约束,您将完全定义ListView的大小。


0
投票

我无法弄清楚这里出了什么问题,但我也不知道当有Recycler视图时为什么要使用ListView。我将为您发布一个示例回收者查看代码。

MainFragment.java:

public class MainFragment.java extends Fragment {

     private RecyclerView recyclerView;
     private CustomAdapter adapter;
     private List<YourObject> objectsList;


     @Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_layout, container, false);

    recyclerView = view.findViewById(R.id.recycler_view);

    //add dummy data
    objectsList = new ArrayList<>();
    objectsList.add(new YourObject());
    objectsList.add(new YourObject());
    objectsList.add(new YourObject());
    objectsList.add(new YourObject());


    adapter = new CustomAdapter(this, objectsList);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.setAdapter(adapter);

    return view;
    }

}

CustomAdapter.java:

    public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {


private Context context;
private List<YourObject> objectsList;

public TransactionListAdapter(Context context, List<Transaction> transactionList) {
    this.context = context;
    this.transactionsList = transactionList;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_layout, parent, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    final YourObject object = objectsList.get(position);

    holder.textView.setText("Stuff here");
    holder.imageView.setImageResource(R.id.myNiceImage);

}

class ViewHolder extends RecyclerView.ViewHolder {

    private ImageView imageView;
    private TextView textView;

    ViewHolder(@NonNull View view) {
        super(view);

        imageView.findViewById(R.id.image_view);
        textView.findViewById(R.id.text_view);

    }
}
}

recycler_item_layout.xml:

   <LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:orientation="hotizontal"
     android:layout_width="match_parent"
     android:layout_height="wrap_content">


            <ImageView
                android:id="@+id/image_view"
                android:layout_width="50dp"
                android:layout_height="50dp"/>

            <TextView
                android:id="@+id/image_view"
                android:layout_width="50dp"
                android:layout_height="50dp"/>

</LinearLayout>

fragment_layout.xml:

   <LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:orientation="hotizontal"
     android:layout_width="match_parent"
     android:layout_height="wrap_content">


      <androidx.recyclerview.widget.RecyclerView
         android:id="@+id/recycler_view"
         android:layout_width="match_parent"
         android:layout_height="match_parent"/>

</LinearLayout>

这比ListView更好并且易于实现

© www.soinside.com 2019 - 2024. All rights reserved.