水平显示Listview

时间:2017-01-04 18:32:14

标签: android listview

resources_activity.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文件,我想看到列表视图“水平”这是我试图在应用程序的资源屏幕上看到的布局

list_single_row.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,我将视图或项目添加到listview

适配器的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但无法实现的所有文件。我已经尝试了一切,请帮忙。

2 个答案:

答案 0 :(得分:1)

您可以使用RecyclerView设置水平LinearLayoutManager,如下所示:

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

答案 1 :(得分:0)

请考虑使用回收站视图。这是解决这个问题的现代方法。这就是我做横向Recycler视图的方式。

    <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);