web-dev-qa-db-ja.com

画面に一度に1枚のカードのみを使用してRecyclerViewを使用して水平CardViewを作成する方法

私はAndroid開発で新しいです。私はカードのリストを画面に表示しようとしていますRecyclerView。しかし、残念ながら、私が設計したカードは、以下に示すimageを見るとわかるように、期待どおりに画面全体に表示されません。これらのカードを拡大する方法画面全体を埋める(幅方向、高さは折り返しコンテンツ)この問題の解決策はありますか?

私のコード:

activity_sub.xml

   <LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    Android:orientation="vertical">

<include
    Android:id="@+id/appBar"
    layout="@layout/app_bar"/>

    <TextView
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        Android:text="Horizontal Card List"
        Android:textSize="16sp"
        Android:padding="3dp"
        Android:background="#CCCCCC" />

    <Android.support.v7.widget.RecyclerView
        Android:id="@+id/hrlist_recycler_view"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        Android:scrollbars="horizontal" />

</LinearLayout>

custom_card.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="wrap_content"
    Android:orientation="vertical"
    Android:padding="8dp"
    Android:tag="contains Cards main Container">

    <Android.support.v7.widget.CardView xmlns:Android="http://schemas.Android.com/apk/res/Android"
        xmlns:app="http://schemas.Android.com/apk/res-auto"
        Android:id="@+id/card_view"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        Android:elevation="10dp"
        app:cardBackgroundColor="#B6B6B6">

        <TextView
            Android:id="@+id/card_text"
            Android:layout_width="wrap_content"
            Android:layout_height="wrap_content"
            Android:layout_gravity="center_vertical"
            Android:padding="20dp"
            Android:text="Card 1"
            Android:textSize="30sp" />

    </Android.support.v7.widget.CardView>
</LinearLayout>

SubActivity.Java

package ab9.mamv.com.playground;

import Android.os.Bundle;
import Android.support.v4.app.NavUtils;
import Android.support.v7.app.ActionBarActivity;
import Android.support.v7.widget.LinearLayoutManager;
import Android.support.v7.widget.RecyclerView;
import Android.support.v7.widget.Toolbar;
import Android.view.Menu;
import Android.view.MenuItem;

import Java.util.ArrayList;
import Java.util.List;


public class SubActivity extends ActionBarActivity {

    Toolbar toolbar;

    private RecyclerView mRecyclerView;
    private SubActivityAdapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

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

        toolbar = (Toolbar) findViewById(R.id.appBar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        //RecyclerView
        mRecyclerView = (RecyclerView) findViewById(R.id.hrlist_recycler_view);
        mRecyclerView.setHasFixedSize(true);

        mAdapter = new SubActivityAdapter(this, getData());
        mRecyclerView.setAdapter(mAdapter);

        //Layout manager for the Recycler View
        mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        mRecyclerView.setLayoutManager(mLayoutManager);


    }

    public static List<SubActivityData> getData() {
        List<SubActivityData> subActivityData = new ArrayList<>();
        String[] cardTitle = {
                "Card 1",
                "Card 2",
                "Card 3",
                "Card 4",
                "Card 5",
                "Card 6",
        };
        for (int i = 0; i < cardTitle.length; i++) {
            SubActivityData current = new SubActivityData();
            current.cardTitle = cardTitle[i];
            subActivityData.add(current);
        }

        return subActivityData;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_sub, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        if (id == Android.R.id.home) {
            NavUtils.navigateUpFromSameTask(this);
        }

        return super.onOptionsItemSelected(item);
    }
}

SubActivityAdapter.Java

package ab9.mamv.com.playground;

import Android.content.Context;
import Android.support.v7.widget.RecyclerView;
import Android.view.LayoutInflater;
import Android.view.View;
import Android.view.ViewGroup;
import Android.widget.TextView;

import Java.util.Collections;
import Java.util.List;

/**
 * Created by Abhishek on 20-03-2015.
 */
public class SubActivityAdapter extends RecyclerView.Adapter<SubActivityAdapter.SubActivityViewHolder> {

    private final LayoutInflater inflater;
    List<SubActivityData> subActivityData = Collections.EMPTY_LIST;

    public SubActivityAdapter(Context context, List<SubActivityData> subActivityData) {
        inflater = LayoutInflater.from(context);
        this.subActivityData = subActivityData;
    }


    @Override
    public SubActivityViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = inflater.inflate(R.layout.custom_card, parent, false);
        SubActivityViewHolder subActivityViewHolder = new SubActivityViewHolder(view);
        return subActivityViewHolder;
    }

    @Override
    public void onBindViewHolder(SubActivityViewHolder holder, int position) {
        SubActivityData currentCard = subActivityData.get(position);
        holder.title.setText(currentCard.cardTitle);

    }

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

    class SubActivityViewHolder extends RecyclerView.ViewHolder {

        TextView title;

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

            title = (TextView) itemView.findViewById(R.id.card_text);
        }
    }
}

enter image description here

15
aB9

OnCreateViewHolderを次のように変更するだけです。

@Override
public SubActivityViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = inflater.inflate(R.layout.custom_card, parent, false);
    view.setMinimumWidth(parent.getMeasuredWidth());
    SubActivityViewHolder subActivityViewHolder = new SubActivityViewHolder(view);
    return subActivityViewHolder;
}

この後、custom_card.xmlで作業する必要がある場合があります。しかし、それでうまくいくはずです。

12

この質問と他の多くの同様の質問がずっと前に投稿されましたが、私はそれに対する単一の実用的な修正を見つけられませんでした。

ここに簡単な修正があります

RecyclerView rv = (RecyclerView) findViewById(R.id.swipeView);;
rv.addOnScrollListener(new RecyclerView.OnScrollListener() {

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);
    LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
    if (dx >= 0) {
        recyclerView.smoothScrollToPosition(layoutManager.findLastVisibleItemPosition());
    } else {
        recyclerView.smoothScrollToPosition(layoutManager.findFirstVisibleItemPosition());
    }
}
});

より魅力的なスワップのためにdxしきい値を追加できます。

1
Muthu Vignesh