将recyclerview和cardview添加到导航按钮片段

时间:2018-06-06 18:45:42

标签: android debugging android-recyclerview crash android-cardview

我正在尝试在导航按钮片段中创建Recyclerview + Cardview,旨在显示一些引用的介绍。系统没有显示任何错误,但它在模拟器中打开时立即崩溃。是否可以在片段中构建这两个视图?如果是这样,有什么问题?非常感谢!

这是MainActivity的代码

 import android.os.Bundle;
 import android.support.annotation.NonNull;
 import android.support.design.widget.BottomNavigationView;
 import android.support.v4.app.FragmentTransaction;
 import android.support.v7.app.AppCompatActivity;
 import android.support.v7.widget.GridLayoutManager;
 import android.support.v7.widget.RecyclerView;
 import android.view.MenuItem;
 import android.widget.TextView;

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


 public class MainActivity extends AppCompatActivity {

private TextView mTextMessage;
List<City> cityList;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {

  //three navigation bottom

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_home:
                setTitle("Home");
                FragmentHome fragmentHome = new FragmentHome();
                FragmentTransaction fragmentTransactionhome = getSupportFragmentManager().beginTransaction();
                fragmentTransactionhome.replace(R.id.fram,fragmentHome, "name");
                fragmentTransactionhome.commit();

                return true;

            case R.id.navigation_tools:
                setTitle("Tools");
                FragmentTools fragmentTools = new FragmentTools();
                FragmentTransaction fragmentTransactionTools = getSupportFragmentManager().beginTransaction();
                fragmentTransactionTools.replace(R.id.fram,fragmentTools, "name");
                fragmentTransactionTools.commit();
                return true;

            case R.id.navigation_notifications:
                setTitle("Notification");
                FragmentNotification fragmentNotification = new FragmentNotification();
                FragmentTransaction fragmentTransactionnotification = getSupportFragmentManager().beginTransaction();
                fragmentTransactionnotification.replace(R.id.fram,fragmentNotification, "name");
                fragmentTransactionnotification.commit();
                return true;
        }
        return false;
    }
};

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

    mTextMessage = (TextView) findViewById(R.id.message);
    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    setTitle("Home");
    FragmentHome fragmentHome = new FragmentHome();
    FragmentTransaction fragmentTransactionhome = getSupportFragmentManager().beginTransaction();
    fragmentTransactionhome.replace(R.id.fram,fragmentHome, "name");
    fragmentTransactionhome.commit();

 //fill the list data
    cityList =new ArrayList<>();

    cityList.add(new City("Taipei","introtesttaipei","culturetesttaipei","accesstesttaipei","tourismtesttaipei", R.drawable.taipei ));

    cityList.add(new City("Tokyo","introtesttokyo","culturetesttokyo","accesstesttokyo","tourismtesttokyo", R.drawable.tokyo ));

    cityList.add(new City("Hongkong","introtesthongkong","culturetesthongkong","accesstesthongkong","tourismtesthongkong", R.drawable.hongkong ));

  //add the recyclerview, and I think the problem has happened here.

    RecyclerView myrecyclerview = (RecyclerView) findViewById(R.id.recyclerview_id);
    RecyclerviewAdapter myAdapter = new RecyclerviewAdapter(this,cityList);
    myrecyclerview.setLayoutManager(new GridLayoutManager(this,3));
    myrecyclerview.setAdapter(myAdapter);

}}

适配器代码

  import android.content.Context;
  import android.support.annotation.NonNull;
  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 java.util.List;

  public class RecyclerviewAdapter extends RecyclerView.Adapter<RecyclerviewAdapter.RecyclerViewHolder> {

private Context mContext;
private List<City> mCityList;

public RecyclerviewAdapter(Context mContext, List<City> mCityList) {
    this.mContext = mContext;
    this.mCityList = mCityList;
}

@NonNull
@Override
public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view;
    LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view= layoutInflater.inflate(R.layout.cardview_items,parent,false);
    RecyclerViewHolder recyclerviewholder = new RecyclerViewHolder(view);
    return recyclerviewholder ;
}

@Override
public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {

    holder.city_title_textview.setText(mCityList.get(position).getTitle());
    holder.city_image_imageview.setImageResource(mCityList.get(position).getSchematic());
}

@Override
public int getItemCount() {
    return mCityList.size();
}

public static class RecyclerViewHolder extends RecyclerView.ViewHolder{


    TextView city_title_textview;
    ImageView city_image_imageview;


    public RecyclerViewHolder(View itemView) {
        super(itemView);

    city_title_textview= itemView.findViewById(R.id.card_title_one);
    city_image_imageview= itemView.findViewById(R.id.card_image_one);

    }
}}

0 个答案:

没有答案