android 水平显示列表视图

vsnjm48y  于 2022-11-20  发布在  Android
关注(0)|答案(2)|浏览(166)

xml文件:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/resources_activity"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.example.a129346.applicationpoc.activities.ResourcesActivity">

        <android.support.v7.widget.Toolbar

            android:id="@+id/my_awesome_resource_toolbar"
            android:layout_height="50dp"
            android:layout_width="match_parent"
            android:background="@drawable/toolbarbackground"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true">

            <ImageView
                android:id="@+id/back_icon_resources"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/back_ic"/>

            <ImageView
                android:id="@+id/pophealth_ic_resources"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:src="@drawable/vehicle_health"/>

            <TextView
                android:id="@+id/vechile_resources_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:textColor="@color/colorPeace"
                android:text="@string/resources"
                android:layout_alignParentTop="true"
                android:layout_toStartOf="@+id/menu_pop_iv"
                android:layout_marginEnd="91dp"
                android:layout_marginTop="12dp" />

            <ImageView
                android:id="@+id/vechile_resources_logout_iv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_power_settings_new_white_24dp"
                android:layout_marginEnd="40dp"
                android:layout_marginLeft="90dp"
                android:layout_alignParentTop="true"
                android:layout_alignParentEnd="true"
                android:layout_marginTop="12dp" />

            <ImageView
                android:id="@+id/menu_resources_iv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="140dp"
                android:src="@drawable/ic_menu_white_24dp"
                android:layout_marginEnd="10dp"
                android:layout_alignTop="@+id/vechile_health_logout_iv"
                android:layout_alignParentEnd="true" />

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

       <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ListView
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:id="@+id/List_view_resources"
                android:layout_marginBottom="13dp"
                >
            </ListView>

        </LinearLayout>
    </LinearLayout>

这是我的xml文件,我想在其中看到列表视图“水平”这是我试图在应用程序的资源屏幕上看到的布局
xml文件:

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

<!--this is the serving layout for the adapter by which i am trying to add items in the list view -->    
    <ImageView
        android:id="@+id/imageview_resources"
        android:layout_width="50dp"
        android:layout_height="50dp" />

    <TextView
        android:id="@+id/text_view_resources"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

这是我用来将视图或项添加到列表视图的xml
适配器的Myresourcesadapter.class文件:

public class Myresourcesadapter extends ArrayAdapter<String> {

    private final Activity context;
    private final String[] itemName;
    private final Integer[] imageResources;

    // created constructor    
    public Myresourcesadapter(Activity context, String[] itemName, Integer[] imageResources) {
        super(context, R.layout.list_single_row,itemName);
        this.context = context;
        this.itemName = itemName;
        this.imageResources = imageResources;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView != null){

        } else {
            LayoutInflater inflater =  context.getLayoutInflater();
            convertView = inflater.inflate(R.layout.list_single_row,null);
            TextView textView_resource = (TextView) convertView.findViewById(R.id.text_view_resources);
            ImageView imageView_resources = (ImageView) convertView.findViewById(R.id.imageview_resources);

            String data = itemName[position];
            textView_resource.setText(data);
            imageView_resources.setImageResource(imageResources[position]);
        }

        return convertView;
    }
}

ResourcesActivity.java:

public class ResourcesActivity extends AppCompatActivity {

        private static String TAG = ResourcesActivity.class.getSimpleName();

        ListView listView_resources;
        Context context_resources;
        ArrayList proglist;

        Integer[] imageResources={
                R.drawable.video,
                R.drawable.manual,
                R.drawable.faqs,
                R.drawable.multimedia,
                R.drawable.map
        };

        String [] itemName = {
                "Video" , "Manual" , "faqs" , "Multimedia" , "maps"
        };
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.resources_activity);
            Myresourcesadapter myresourcesadapter = new Myresourcesadapter(this,itemName,imageResources);

            listView_resources = (ListView) findViewById(R.id.List_view_resources);

            listView_resources.setAdapter(myresourcesadapter);
        } 
    }

这些都是我的文件,我试图实现listview水平,但不能。我已经尝试了一切,请帮助。

mcvgt66p

mcvgt66p1#

考虑使用RecyclerView,它的功能更强大,并且支持本机水平滚动。
您可以通过设置水平LinearLayoutManager来使用RecyclerView,如下所示:

LinearLayoutManager layoutManager
    = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView myRecyclerView = findViewById(R.id.recycler_view);
myRecyclerView.setLayoutManager(layoutManager);
oxcyiej7

oxcyiej72#

考虑使用回收器视图。这是解决这个问题的现代方法。这是我如何做一个水平回收器视图。

<android.support.v7.widget.RecyclerView
    android:id="@+id/custom_tab_bar"
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:orientation="horizontal"
    android:paddingTop="20dp"
    app:layoutManager="android.support.v7.widget.LinearLayoutManager"
    tools:listitem="@layout/adapter_tab_layout"
/>

mLayoutManager = new CustomLayoutManager(this); // custom for smooth scrolling... try mLayoutManager = new LinearLayoutManager(getApplicationContext());
mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
mCustomTabBarRecyclerView.setAdapter(mCustomTabAdapter);
mCustomTabBarRecyclerView.setHasFixedSize(true);
mCustomTabBarRecyclerView.setLayoutManager(mLayoutManager);

相关问题