在RecycleViewAdapter中使用CardView的动态片段

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

我在Android开发领域是全新的,当CardView是片段时,我很难理解RecycleViewAdapter如何与CardView一起工作。我试图理解https://github.com/firebase/quickstart-android/blob/master/database/README.md上的例子,但由于他们使用了许多与Firebase相关的东西,我试图将其移植到我自己的例子中。

我想要一个包含几张卡的列表,如下所示:

enter image description here

但是,用户应该能够添加和删除不同的卡。在开始时,视图应为空,如果用户按下右上角的浮动按钮

enter image description here

应该创建一张新卡。

enter image description here

我刚刚移植了我认为它应该工作的方式。不幸的是,当我按下按钮时没有发生任何事情(当然打印了Hello World)。

谢谢你的帮助。

这是我的MainActivity的代码:

package com.example.dynamicfragment;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends AppCompatActivity
{

    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fragmentManager = getFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

       FloatingActionButton fab = findViewById(R.id.fab);
       fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                System.out.println("Hello World");
                fragmentTransaction.add(new CardListFragment(), "CardListFragment");
            }
        });
        fragmentTransaction.commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        int id = item.getItemId();
        if (id == R.id.action_settings)
        {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
  }

这是扩展CardListFragment的类:

  package com.example.dynamicfragment;


  import android.os.Bundle;
  import android.app.Fragment;
  import android.support.v7.widget.LinearLayoutManager;
  import android.support.v7.widget.RecyclerView;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;

  import com.example.dynamicfragment.adapter.CardAdapter;
  import com.example.dynamicfragment.model.Party;

  import java.util.ArrayList;

  public class CardListFragment extends Fragment
  {
      private static final String TAG = "CardListFragment";

      private RecyclerView mRecycler;
      private LinearLayoutManager mManager;
      private CardAdapter mAdapter;

      @Override
      public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
      {
          super.onCreateView(inflater, container, savedInstanceState);
          View rootView = inflater.inflate(R.layout.fragment_card_list, container, false);
          mRecycler = rootView.findViewById(R.id.messagesList);
          mRecycler.setHasFixedSize(true);
          ArrayList<Party> demo = new ArrayList<>();
          demo.add(new Party(0,"Demo", R.drawable.ic_action_account_circle_40));
          this.mAdapter = new CardAdapter(demo);
          return rootView;
      }

      @Override
      public void onActivityCreated(Bundle savedInstanceState)
      {
          super.onActivityCreated(savedInstanceState);
          mManager = new LinearLayoutManager(getActivity());
          mManager.setReverseLayout(true);
          mManager.setStackFromEnd(true);
          mRecycler.setLayoutManager(mManager);
          mRecycler.setAdapter(null);
      }

      @Override
      public void onStart()
      {
          super.onStart();
          if (null != mAdapter)
          {
              //mAdapter.startListening();
          }
      }

      @Override
      public void onStop()
      {
          super.onStop();
          if (null != mAdapter)
          {
              //mAdapter.stopListening();
          }
      }

  }

这是我的数据模型的代码:

package com.example.dynamicfragment.model;

import java.util.Objects;

public class Party
{
    private int id;
    private String name;
    private int image;

    public Party(int id, String name, int image)
    {
        this.id = id;
        this.name = name;
        this.image = image;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getImage() {
        return image;
    }

    public void setImage(int image) {
        this.image = image;
    }

    @Override
    public boolean equals(Object o)
    {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Party party = (Party) o;
        return id == party.id;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

}

这是我的Adapter类:

package com.example.dynamicfragment.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.dynamicfragment.R;
import com.example.dynamicfragment.model.Party;

import java.util.ArrayList;

public class CardAdapter extends RecyclerView .Adapter<CardAdapter.CardViewHolder>
{

    private ArrayList<Party> dataList;

    public static class CardViewHolder extends RecyclerView.ViewHolder
    {
        TextView partyName;
        ImageView imageViewIcon;

        public CardViewHolder(View itemView)
        {
            super(itemView);
            this.partyName = (TextView) itemView.findViewById(R.id.postAuthor);
            this.imageViewIcon = (ImageView)     itemView.findViewById(R.id.postAuthorPhoto);
        }
    }

    public CardAdapter(ArrayList<Party> data)
    {
        this.dataList = data;
    }

    @Override
    public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.card_view, parent, false);

        return new CardViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final CardViewHolder holder, final int    listPosition)
    {
        TextView partyName = holder.partyName;
        ImageView imageView = holder.imageViewIcon;
        partyName.setText(dataList.get(listPosition).getName());
        imageView.setImageResource(dataList.get(listPosition).getImage());
    }

    @Override
    public int getItemCount()
    {
        return dataList != null ? dataList.size() : 0;
    }

}

内容文件:

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

content_main.xml:

<?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">

    <include layout="@layout/fragment_card_list" />

</android.support.constraint.ConstraintLayout>

fragment_card_list.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/messagesList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:clipToPadding="false"
        android:padding="5dp"
        android:scrollbars="vertical"
        tools:listitem="@layout/card_view" />

</FrameLayout>

card_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp">

        <include
            android:id="@+id/postAuthorLayout"
            layout="@layout/include_post_author"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true" />

        <LinearLayout
            android:id="@+id/starLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/postAuthorLayout"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/postAuthorLayout"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/star"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="5dp"
                android:background="?attr/selectableItemBackground"
                android:src="@drawable/ic_toggle_star_outline_24" />

            <TextView
                android:id="@+id/postNumStars"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                tools:text="7" />

        </LinearLayout>

        <include layout="@layout/include_post_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/postAuthorLayout"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="10dp" />

    </RelativeLayout>

</android.support.v7.widget.CardView>

include_post_author.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/postAuthorPhoto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_action_account_circle_40" />

    <TextView
        android:id="@+id/postAuthor"
        style="@style/Base.TextAppearance.AppCompat.Small"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:gravity="center_vertical"
        tools:text="[email protected]" />

</LinearLayout>

include_post_text.xml:

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

    <TextView
        android:id="@+id/postTitle"
        style="@style/TextAppearance.AppCompat.Medium"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        android:textStyle="bold"
        tools:text="My First Post" />

    <TextView
        android:id="@+id/postBody"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        tools:text="Hallo Welt" />

</LinearLayout>
android android-recyclerview android-adapter android-cardview fragmentmanager
1个回答
0
投票

@Christoph M.片段交易,如果你想申请你,你需要提供一个FrameLayout Id。

在主类中创建一个带有一个id的FrameLayout,然后将您的片段附加到id值

如下所示

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

      FirstFragment  fragment = new FirstFragment();
        FragmentTransaction transaction = 
         getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_layout,  fragment);
        transaction.addToBackStack(ConstantVariables.FirstFragment);
        transaction.commit();
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/frame_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>
© www.soinside.com 2019 - 2024. All rights reserved.