美好的一天, 我有一个应用程序,主要活动,碎片和标签碎片。我正在尝试使用Cardview显示我的Allposts片段上的帖子列表。但是,当我运行它时,没有任何显示。
MainActivity.java
package com.example.stephan.regnotifier;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import com.example.stephan.regnotifier.activity.AboutFragment;
import com.example.stephan.regnotifier.activity.PostsFragment;
import com.example.stephan.regnotifier.activity.SettingsFragment;
import com.example.stephan.regnotifier.activity.TabFragment;
import com.example.stephan.regnotifier.activity.TimetableFragment;
public class MainActivity extends AppCompatActivity{
DrawerLayout mDrawerLayout;
NavigationView mNavigationView;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
*Setup the DrawerLayout and NavigationView
*/
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;
/**
* Lets inflate the very first fragment
* Here , we are inflating the TabFragment as the first Fragment
*/
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();
/**
* Setup click events on the Navigation View Items.
*/
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
mDrawerLayout.closeDrawers();
if (menuItem.getItemId() == R.id.nav_item_timetable) {
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.containerView,new TimetableFragment()).commit();
}
if (menuItem.getItemId() == R.id.nav_item_post) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();
}
if (menuItem.getItemId() == R.id.nav_item_profile) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new PostsFragment()).commit();
}
if (menuItem.getItemId() == R.id.nav_item_settings) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new SettingsFragment()).commit();
}
if (menuItem.getItemId() == R.id.nav_item_about) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new AboutFragment()).commit();
}
if (menuItem.getItemId() == R.id.nav_item_logout) {
Intent logout = new Intent(MainActivity.this, LoginActivity.class);
startActivity(logout);
}
return false;
}
});
/**
* Setup Drawer Toggle of the Toolbar
*/
android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout, toolbar,R.string.app_name,
R.string.app_name);
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
}
}
AllpostsFragment.java
package com.example.stephan.regnotifier.activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.CardView;
import android.support.v7.widget.DefaultItemAnimator;
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.stephan.regnotifier.R;
/**
* A simple {@link Fragment} subclass.
*/
public class AllpostsFragment extends Fragment {
public AllpostsFragment() {
// Required empty public constructor
}
CardView mCardView;
RecyclerView mRecyclerView;
RecyclerView.LayoutManager mLayoutManager;
RecyclerView.Adapter mAdapter;
Context context;
public static AllpostsFragment newInstance() {
AllpostsFragment fragment = new AllpostsFragment();
fragment.setRetainInstance(true);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_allposts, container, false);
RecyclerView rv = (RecyclerView)v.findViewById(R.id.cardList);
mLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
rv.setLayoutManager(mLayoutManager);
rv.setItemAnimator(new DefaultItemAnimator());
rv.setHasFixedSize(true);
return v;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
PostList.java
package com.example.stephan.regnotifier.model;
/**
* Created by StephAn on 18-Feb-16.
*/
public class Posts_list {
private String mName;
private int mThumbnail;
public String getName() {
return mName;
}
public void setName(String name) {
this.mName = name;
}
public int getThumbnail() {
return mThumbnail;
}
public void setThumbnail(int thumbnail) {
this.mThumbnail = thumbnail;
}
}
CardAdapter.java
package com.example.stephan.regnotifier.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.stephan.regnotifier.R;
import com.example.stephan.regnotifier.model.Posts_list;
import java.util.ArrayList;
import java.util.List;
/**
* Created by StephAn on 18-Feb-16.
*/
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.CardViewHolder>{
List<Posts_list> pItems;
public CardAdapter() {
super();
pItems = new ArrayList<Posts_list>();
Posts_list posts_list = new Posts_list();
posts_list.setName("The Amazing Spider-Man 2");
posts_list.setThumbnail(R.drawable.sample);
pItems.add(posts_list);
posts_list = new Posts_list();
posts_list.setName("X-men: Days of Future Past");
posts_list.setThumbnail(R.drawable.sample);
pItems.add(posts_list);
posts_list = new Posts_list();
posts_list.setName("The Hunger Game");
posts_list.setThumbnail(R.drawable.sample);
pItems.add(posts_list);
posts_list = new Posts_list();
posts_list.setName("Guardians of the Galaxy");
posts_list.setThumbnail(R.drawable.sample);
pItems.add(posts_list);
posts_list = new Posts_list();
posts_list.setName("Maleficent");
posts_list.setThumbnail(R.drawable.sample);
pItems.add(posts_list);
posts_list = new Posts_list();
posts_list.setName("How to Train Your Dragon 2");
posts_list.setThumbnail(R.drawable.sample);
pItems.add(posts_list);
posts_list = new Posts_list();
posts_list.setName("What If");
posts_list.setThumbnail(R.drawable.sample);
pItems.add(posts_list);
}
@Override
public CardViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.card_view_layout, viewGroup, false);
CardViewHolder viewHolder = new CardViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(CardViewHolder cardViewHolder, int i) {
Posts_list posts_list = pItems.get(i);
cardViewHolder.tvMovie.setText(posts_list.getName());
cardViewHolder.imgThumbnail.setImageResource(posts_list.getThumbnail());
}
@Override
public int getItemCount() {
return pItems.size();
}
class CardViewHolder extends RecyclerView.ViewHolder{
public ImageView imgThumbnail;
public TextView tvMovie;
public CardViewHolder(View itemView) {
super(itemView);
imgThumbnail = (ImageView)itemView.findViewById(R.id.img_thumbnail);
tvMovie = (TextView)itemView.findViewById(R.id.tv_movie);
}
}
}
fragment_allpost.xml
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MyActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/cardList"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
card_view_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
card_view:contentPadding="@dimen/activity_horizontal_margin"
card_view:cardCornerRadius="3dp">
<ImageView
android:id="@+id/img_thumbnail"
android:layout_width="fill_parent"
android:layout_height="150dp" />
<TextView
android:id="@+id/tv_movie"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:layout_gravity="bottom"
android:gravity="center_vertical"
android:background="#757575"
android:alpha="0.8"
android:textSize="@dimen/abc_text_size_headline_material"
android:text="Test"/>
</android.support.v7.widget.CardView>
</LinearLayout>