CardView不在RecyclerView中充气

时间:2016-12-28 21:14:40

标签: android android-recyclerview

我无法弄清楚为什么我在运行应用程序时看不到CardView。它显示在Android Studio的设计窗口中,但是当我运行应用程序时,没有出现任何错误,并且应用程序正常运行,但CardView没有显示。

在发布我的代码之前,我正在尝试研究,看看我是否犯了同样的错误:

CardView not shown in RecyclerView

card view not showing up

(显然,我需要有超过10个声望才能发布超过2个链接,只知道大约有10个)

请知道我对开发和RecyclerView相对较新,并且我真的做了尝试google / stackoverflow以获得答案,我尝试实施所有这些,但没有一个对我有用的。要么它是我没有看到的东西,要么我只是没有足够的经验注意到。不管怎样,我真的很感激任何帮助。

以下是我的gradle依赖项:

ext {
    // versions for libraries that multiple dependencies
    supportLibVersion = '25.1.0'
    dagger = '2.7'
    butterknife = '8.4.0'
    pocketknife = '3.2.1'
}

dependencies {
    // Support Libraries
    compile "com.android.support:appcompat-v7:${supportLibVersion}"
    compile "com.android.support:design:${supportLibVersion}"
    compile "com.android.support:support-v4:${supportLibVersion}"
    compile "com.android.support:cardview-v7:${supportLibVersion}"
    compile "com.android.support:recyclerview-v7:${supportLibVersion}"
    //recyclerview from brian wernick
    compile 'com.devbrackets.android:recyclerext:2.0.1'

以下是MainActivity:

public class MainActivity extends BaseActivity implements BottomNavigationView.OnNavigationItemSelectedListener, ViewPager.OnPageChangeListener {
    @Inject
    EventBus bus;

    @BindView(R.id.bottomNavigation)
    BottomNavigationView bottomNavigation;
    @BindView(R.id.viewpager)
    ViewPager viewPager;

    private SwipeFragmentPagerAdapter adapter;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_main;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Injector.get().inject(this);
        PocketKnife.restoreInstanceState(this, savedInstanceState);
        bottomNavigation.setOnNavigationItemSelectedListener(this);
        adapter = new SwipeFragmentPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(this);
        bus.postSticky(new ViewPagerEvent(adapter.getItem(0).getClass()));
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int position;
        if (item.getItemId() == R.id.menu_item_explore) {
            position = 1;
        } else if (item.getItemId() == R.id.menu_item_my_goals) {
            position = 2;
        } else if (item.getItemId() == R.id.menu_item_profile) {
            position = 3;
        } else {
            position = 0;
        }
        viewPager.setCurrentItem(position, false);
        return true;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        //required empty method
    }

    @Override
    public void onPageSelected(int position) {
        MenuItem menuItem = bottomNavigation.getMenu().getItem(position);
        if (menuItem != null) {
            menuItem.setChecked(true);
            bus.postSticky(new ViewPagerEvent(adapter.getItem(position).getClass()));
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        //required empty method
    }
}

这是我的适配器:

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.ViewGroup;

import com.devbrackets.android.recyclerext.adapter.RecyclerListAdapter;
import com.devbrackets.android.recyclerext.adapter.viewholder.ClickableViewHolder;

import agency.rain.android.simple.things.model.ExploreData;
import agency.rain.android.simple.things.ui.activity.ClickActivity;
import agency.rain.android.simple.things.ui.activity.adapter.viewholder.ExploreViewHolder;

public class ExploreAdapter extends RecyclerListAdapter<ExploreViewHolder, ExploreData> implements ClickableViewHolder.OnClickListener {

    private Context context;
    public ExploreAdapter(Context context) {
        this.context = context;
    }

    @Override
    public ExploreViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return ExploreViewHolder.newInstance(parent, this);
    }

    @Override
    public void onBindViewHolder(ExploreViewHolder holder, int position) {
        final ExploreData data = getItem(position);
        if (data == null) {
            return;
        }
        holder.bindData(data);
    }

    @Override
    public void onClick(@NonNull ClickableViewHolder viewHolder) {
        final ExploreData data = getItem(viewHolder.getAdapterPosition());
        Intent repListIntent = new Intent(context, ClickActivity.class);
        context.startActivity(repListIntent);
    }
}

这是我的ViewHolder:

import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.devbrackets.android.recyclerext.adapter.viewholder.ClickableViewHolder;

import agency.rain.android.simple.things.R;
import agency.rain.android.simple.things.model.ExploreData;
import butterknife.BindView;
import butterknife.ButterKnife;

public class ExploreViewHolder extends ClickableViewHolder {
    @BindView(R.id.card_view_explore)
    CardView cv;
    @BindView(R.id.explore_placeholder_text)
    TextView ept;
    @BindView(R.id.explore_placeholder_image)
    ImageView epi;

    private ExploreViewHolder(@NonNull View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }
    public static ExploreViewHolder newInstance(ViewGroup parent, ClickableViewHolder.OnClickListener listener) {
        final ExploreViewHolder viewHolder = new ExploreViewHolder(
                LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_explore, parent, false));
        viewHolder.setOnClickListener(listener);
        return viewHolder;
    }
    public void bindData(ExploreData data) {

        ept.setText(data.getExploreText());
        epi.setImageResource(data.getExploreImage());
        }
}

这是我的数据对象:

import java.io.Serializable;

public class ExploreData implements Serializable {
    private String exploreText;
    private int exploreImage;

    public int getExploreImage() {
        return exploreImage;
    }

    public String getExploreText() {
        return exploreText;
    }
}

这是应显示CardView的片段:

   public class ExploreFragment extends BaseFragment implements AdapterView.OnItemClickListener {
        @BindView(R.id.explore_recycler_view)
        RecyclerView exploreRecyclerView;

        private Unbinder unbinder;
        private ExploreAdapter exploreAdapter;


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_explore, container, false);
            Injector.get().inject(this);
            unbinder = ButterKnife.bind(this, view);
            PocketKnife.bindArguments(this);
            final LinearLayoutManager exploreLayoutManager = new LinearLayoutManager(getActivity());
            exploreRecyclerView.setLayoutManager(exploreLayoutManager);
            exploreAdapter = new ExploreAdapter(getActivity());
            exploreRecyclerView.setAdapter(exploreAdapter);
            return view;
        }

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

        @Override
        public void onDestroy() {
            super.onDestroy();
            if (unbinder != null) {
                unbinder.unbind();
            }
        }

        @Override
        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            inflater.inflate(R.menu.menu_explore, menu);
        }

        @Override
        protected int getTitleResourceId() {
            return R.string.explore_title_text;
        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        }
    }

片段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="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/explore_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" />

</LinearLayout>

最后但并非最不重要的是,list_item xml:

<?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view_explore"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        card_view:cardCornerRadius="4dp"
        android:clickable="true"
        card_view:cardBackgroundColor="@android:color/white"
        android:background="?android:attr/selectableItemBackground"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/explore_placeholder_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/drop_the_pounds"
                android:textColor="@android:color/black"
                android:textAllCaps="true" />

            <ImageView
                android:id="@+id/explore_placeholder_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/challengeimage"
                android:layout_marginRight="65dp"
                android:layout_marginEnd="65dp" />

        </RelativeLayout>

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

0 个答案:

没有答案