使用单行在Horizo​​ntal Gridview中显示图像

时间:2018-02-01 05:45:30

标签: android

我想在屏幕顶部的单行中以水平Gridview显示图像。它应该有多个图像,并且应该在图像聚焦时相应地滚动。聚焦的图像应位于屏幕的中心。

由于

Main_activity.xml ******

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
   >

<android.support.v17.leanback.widget.HorizontalGridView
    android:layout_height="80dp"
    android:layout_width="wrap_content"
    android:id="@+id/horview"
    android:background="@color/colorPrimaryDark"
     >
<!--
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/_image"/>-->

</android.support.v17.leanback.widget.HorizontalGridView>
</RelativeLayout>

主要活动.java ****

package com.example.farhanm.horizontal2;

import android.support.v17.leanback.widget.HorizontalGridView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {


    private View hview;
    private ImageView imageView;
    private HorizontalGridView hgview;
    private RelativeLayout relativeLayout;
    private int[] images = {R.color.colorAccent, R.color.colorPrimary, R.color.colorPrimaryDark, R.color.lb_action_text_color,
            R.color.lb_basic_card_bg_color, R.color.colorAccent};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       // relativeLayout=(RelativeLayout)findViewById(R.id.relative1);
        hgview=(HorizontalGridView)findViewById(R.id.horview);
       for (int i = 0; i < images.length; i++) {


           hview =getLayoutInflater().inflate(R.layout.hview, null);
            imageView = (ImageView)hview.findViewById(R.id._image);
            imageView.setImageResource(images[i]);
          //  relativeLayout.addView(imageView);
            hgview.addView(imageView);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以尝试以下代码

,而不是使用此代码
<android.support.v17.leanback.widget.HorizontalGridView
android:layout_height="80dp"
android:layout_width="wrap_content"
android:id="@+id/horview"
android:background="@color/colorPrimaryDark">

RecyclerView添加到XML中的Linearlayout

<LinearLayout
          android:id="@+id/image_LAY"
          android:layout_width="match_parent"
          android:layout_height="wrap_content">

      <android.support.v7.widget.RecyclerView
          android:id="@+id/recycler_view_image"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:padding="3dp" />

    </LinearLayout>

使用LinearLayoutmanager并将其方向设置为horizontal

final LinearLayoutManager layoutManager = new LinearLayoutManager(myContext);
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
ImageRecyclerView.setLayoutManager(layoutManager);

创建RecyclerView适配器以水平添加图像。并将图像数组添加到Recyclerview适配器。

 RecyclerViewAdapter = new STOffsiteRVAdapter(aImageInfos, Context);
 ImageRecyclerView.setAdapter(RecyclerViewAdapter);