使recyclerView项目适合屏幕

时间:2017-02-06 23:36:35

标签: android

我有一个recyclerView,在活动下方显示水平图标,如水平导航栏。

RecyclerView适配器和活动

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_realuser_int);
    Intent intent= new Intent(this,ListenKeyword.class);
    startService(intent);

    horizontal_recycler_view= (RecyclerView) findViewById(R.id.horizontal_recycler_view);
    Horizontalicons = new ArrayList<>();
    Horizontalicons.add(R.drawable.home);
    Horizontalicons.add(R.drawable.clock);
    Horizontalicons.add(R.drawable.records);
    Horizontalicons.add(R.drawable.menu);
    horizontalAdapter=new HorizontalAdapter(Horizontalicons);


    LinearLayoutManager horizontalLayoutManagaer = new LinearLayoutManager(realuserIntActivity.this, LinearLayoutManager.HORIZONTAL, false);
    horizontal_recycler_view.setLayoutManager(horizontalLayoutManagaer);
    horizontal_recycler_view.setAdapter(horizontalAdapter);

    user=(TextView) findViewById(R.id.textUser);

}

public class HorizontalAdapter extends RecyclerView.Adapter<HorizontalAdapter.MyViewHolder>  {
    String[]iconnames={"Home", "History", "History", "Menu"};
    private List<Integer> Horizontalicons;
    public class MyViewHolder extends RecyclerView.ViewHolder {
        public ImageView imgIcons;

        public MyViewHolder(View view) {
            super(view);
            imgIcons = (ImageView) view.findViewById(R.id.imgrecycler);

        }
    }


    public HorizontalAdapter(List<Integer> Horizontalicons) {
        this.Horizontalicons = Horizontalicons;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.iconrecycler, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.imgIcons.setImageResource(Horizontalicons.get(position));

        holder.imgIcons.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(realuserIntActivity.this, iconnames[position], Toast.LENGTH_SHORT).show();
            }
        });
    }

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

XML文件

 <android.support.v7.widget.RecyclerView
    android:id="@+id/horizontal_recycler_view"

    android:background="#263238"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

CUSTOM XML将在回收中充满活力

  <ImageView
    android:layout_width="63dp"
    android:layout_height="63dp"
    android:id="@+id/imgrecycler"
    android:layout_marginTop="9dp"
    android:layout_marginRight="97dp"
    android:paddingLeft="30dp"
    />

我试过在三台不同的设备上运行。我面临的问题是,在一台屏幕较小的设备(手机)上只显示三个图标,看到最后一个图标你会必须向左滚动才能看到最后一项。在更大的屏幕(平板电脑)上显示所有图标或项目而无需滚动。有没有办法让所有项目都适合屏幕,无论使用的屏幕大小。

1 个答案:

答案 0 :(得分:1)

您可以使用imageview的宽度和高度,边距等进行操作。您将不得不创建上下文并将其传递给适配器类的构造函数,以便您可以像这样获取getResources:

 Context context;

传递给构造函数:

public HorizontalAdapter(Context context, List<Integer> Horizontalicons) {
    this.Horizontalicons = Horizontalicons;
    this.context = context;
}

在MyViewHolder中,您可以执行以下操作:

public MyViewHolder(View view) {
        super(view);
        imgIcons = (ImageView) view.findViewById(R.id.imgrecycler);
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)imgIcons.getLayoutParams();

    int screenSize = context.getResources().getConfiguration().screenLayout &
            Configuration.SCREENLAYOUT_SIZE_MASK;

    switch(screenSize) {
        case Configuration.SCREENLAYOUT_SIZE_XLARGE:
        //do stuff
            break;

        case Configuration.SCREENLAYOUT_SIZE_LARGE:
           // do stuff
            break;

        case Configuration.SCREENLAYOUT_SIZE_NORMAL:
         //do stuff
            break;

        case Configuration.SCREENLAYOUT_SIZE_SMALL:
         //for example here

                lp.height = 20;
                lp.width = 20;
                lp.leftMargin = 20;
                lp.rightMargin = 20;
                imgIcons.setLayoutParams(lp);
            break;

        default:
       }
    }

我使用此模式在我的应用和cardviews身高中设置文字大小。