本文介绍了如何有一个水平的ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我想一些图片在一排与水平滚动选项显示的要求。

我试图把一个gridview一个水平滚动视图里面,设置GridView控件对图像的数量列数。但是,这也是没有办法。

请提出好的建议?

 < XML版本=1.0编码=UTF-8&GT?;

< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent机器人:layout_height =match_parent
机器人:方向=垂直机器人:背景=@机器人:彩色/白机器人:可点击=真
>

< Horizo​​ntalScrollView
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:滚动条=无
    机器人:layout_margin =1DP
    机器人:fillViewport =假
    机器人:layout_below =@ + ID / tvHis>
    <的LinearLayout
        机器人:方向=横向
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        >
    < GridView控件


        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:ID =@ + ID / listViewHis
        机器人:layout_below =@ + ID / tvHisPosts



        />
    < / LinearLayout中>
< / Horizo​​ntalScrollView>

< / LinearLayout中>
 

解决方案

我有没有使用任何图书馆终于实现了,看看这个:

我收到了JSON的反响是这样的:

<$p$p><$c$c>{"searchInfo":{"status":"1","message":"Success","clist":[{"id":"1de57434-795e-49ac-0ca3-5614dacecbd4","name":"Theater","image_url":"http://52.25.198.71/miisecretory/category_images/movie.png"},{"id":"62fe1c92-2192-2ebb-7e92-5614dacad69b","name":"CNG","image_url":"http://52.25.198.71/miisecretory/category_images/cng.png"},{"id":"8060094c-df4f-5290-7983-5614dad31677","name":"Wine-shop","image_url":"http://52.25.198.71/miisecretory/category_images/beer.png"},{"id":"888a90c4-a6b0-c2e2-6b3c-561788e973f6","name":"Chemist","image_url":"http://52.25.198.71/miisecretory/category_images/chemist.png"},{"id":"a39b4ec1-943f-b800-a671-561789a57871","name":"Food","image_url":"http://52.25.198.71/miisecretory/category_images/food.png"},{"id":"c644cc53-2fce-8cbe-0715-5614da9c765f","name":"College","image_url":"http://52.25.198.71/miisecretory/category_images/college.png"},{"id":"c71e8757-072b-1bf4-5b25-5614d980ef15","name":"Hospital","image_url":"http://52.25.198.71/miisecretory/category_images/hospital.png"},{"id":"db835491-d1d2-5467-a1a1-5614d9963c94","name":"Petrol-Pumps","image_url":"http://52.25.198.71/miisecretory/category_images/petrol.png"},{"id":"f13100ca-4052-c0f4-863a-5614d9631afb","name":"ATM","image_url":"http://52.25.198.71/miisecretory/category_images/atm.png"}]}}

所以我想创造一个水平滚动视图与合作点击事件,因为我也有借鉴谷歌地图路线的话,首先我画一个布局,作为这样的:

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT; LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直
    机器人:weightSum =5&GT;

    &LT;片段
        机器人:ID =@ + ID /图
        机器人:名称=com.google.android.gms.maps.SupportMapFragment
        机器人:layout_width =match_parent
        机器人:layout_height =0dp
        机器人:layout_weight =4/&GT;


    &LT; Horizo​​ntalScrollView
        机器人:ID =@ + ID / horizo​​ntalScroll
        机器人:layout_width =match_parent
        机器人:layout_height =0dp
        机器人:layout_weight =1&GT;

        &LT;的LinearLayout
            机器人:ID =@ + ID / LL
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            机器人:重力=中心
            机器人:方向=横向&GT;


        &LT; / LinearLayout中&GT;


    &LT; / Horizo​​ntalScrollView&GT;
&LT; / LinearLayout中&GT;
 

现在根据JSON响应它由JSON数组我做到了这一点

 的(INT V = 0; V&LT; collectionInfo.size(); v ++){
                / * ---------------创建框架布局---------------------- * /

                的FrameLayout的FrameLayout =新的FrameLayout(ActivityMap.this);
                LinearLayout.LayoutParams的LayoutParams =新LinearLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,getPixelsToDP(90));
                layoutParams.rightMargin = getPixelsToDP(10);
                frameLayout.setLayoutParams(的LayoutParams);

                / * --------------的框架布局年底---------------------------- * /

                / * ---------------创建图像视图---------------------- * /
                最后ImageView的imgView =新ImageView的(ActivityMap.this); //动态创建的ImageView
                LinearLayout.LayoutParams lpImage =新LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
                imgView.setImageBitmap(collectionInfo.get(ⅴ).getCatImage());
                imgView.setLayoutParams(lpImage);
                //设置ID检索在稍后的时间(同它的位置)
                imgView.setId(五);
                imgView.setOnClickListener(新View.OnClickListener(){
                    @覆盖
                    公共无效的onClick(视图v){
//长ID = imgView.getId();
                        //获取ID,这是与它的位置
                        Log.i(TAG,点击+ collectionInfo.get(v.getId())getCatName());
                        //获得所选类别的数据表
                        新GetSelectedCategoryData()执行(collectionInfo.get(v.getId())getCatID(​​));
                    }
                });
                / * --------------图像视图结束---------------------------- * /

                / * ---------------创建文本视图---------------------- * /
                TextView中的TextView =新的TextView(ActivityMap.this); //动态创建的TextView
                textView.setText(collectionInfo.get(ⅴ).getCatName());
                FrameLayout.LayoutParams lpText =新FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT,Gravity.BOTTOM | Gravity.CENTER);
                //注:LinearLayout.LayoutParams的引力是行不通的,所以我推的FrameLayout为3 paramater是自身重力
                //lpText.gravity = Gravity.BOTTOM | Gravity.CENTER;
                textView.setTextColor(Color.parseColor(#43A047));
                textView.setLayoutParams(lpText);
                / * --------------的文字年底---------------------------- * /

                //添加的意见在适当的地方
                frameLayout.addView(imgView);
                frameLayout.addView(TextView的);
                linearLayout.addView(的FrameLayout);

            }
 

这是在这里工作的伎俩是,我分配到的ImageView id为imgView.setId(五),之后将onClickListener来,我再获取视图的ID ......

在这样,我已经成功地实现了解决方案...我已经开发了这个code几天就回来了,我想我应该和大家一起分享....快乐编码...

I have a requirement in which I want few images to be shown in one row with horizontal scroll option.

I tried putting a gridview inside a horizontal-scrollview, and setting the number for columns of gridview to number of images.But this approach does not help.

Please suggest something?

<?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="match_parent"
android:orientation="vertical" android:background="@android:color/white" android:clickable="true"
>

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="none"
    android:layout_margin="1dp"
    android:fillViewport="false"
    android:layout_below="@+id/tvHis">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
    <GridView


        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listViewHis"
        android:layout_below="@+id/tvHisPosts"



        />
    </LinearLayout>
</HorizontalScrollView>

</LinearLayout>
解决方案

I have finally achieved it without using any library, have a look at this:

I was receiving json responce as this:

{"searchInfo":{"status":"1","message":"Success","clist":[{"id":"1de57434-795e-49ac-0ca3-5614dacecbd4","name":"Theater","image_url":"http://52.25.198.71/miisecretory/category_images/movie.png"},{"id":"62fe1c92-2192-2ebb-7e92-5614dacad69b","name":"CNG","image_url":"http://52.25.198.71/miisecretory/category_images/cng.png"},{"id":"8060094c-df4f-5290-7983-5614dad31677","name":"Wine-shop","image_url":"http://52.25.198.71/miisecretory/category_images/beer.png"},{"id":"888a90c4-a6b0-c2e2-6b3c-561788e973f6","name":"Chemist","image_url":"http://52.25.198.71/miisecretory/category_images/chemist.png"},{"id":"a39b4ec1-943f-b800-a671-561789a57871","name":"Food","image_url":"http://52.25.198.71/miisecretory/category_images/food.png"},{"id":"c644cc53-2fce-8cbe-0715-5614da9c765f","name":"College","image_url":"http://52.25.198.71/miisecretory/category_images/college.png"},{"id":"c71e8757-072b-1bf4-5b25-5614d980ef15","name":"Hospital","image_url":"http://52.25.198.71/miisecretory/category_images/hospital.png"},{"id":"db835491-d1d2-5467-a1a1-5614d9963c94","name":"Petrol-Pumps","image_url":"http://52.25.198.71/miisecretory/category_images/petrol.png"},{"id":"f13100ca-4052-c0f4-863a-5614d9631afb","name":"ATM","image_url":"http://52.25.198.71/miisecretory/category_images/atm.png"}]}}

so I wanted to create a horizontal scrollview with working click events as I was also have to draw routes on google map so, first I draw a layout for that as like this :

<?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="match_parent"
    android:orientation="vertical"
    android:weightSum="5">

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="4" />


    <HorizontalScrollView
        android:id="@+id/horizontalScroll"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <LinearLayout
            android:id="@+id/ll"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="horizontal">


        </LinearLayout>


    </HorizontalScrollView>
</LinearLayout>

now according to json response which consists of json array I done this:

for (int v = 0; v < collectionInfo.size(); v++) {
                /*---------------Creating frame layout----------------------*/

                FrameLayout frameLayout = new FrameLayout(ActivityMap.this);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, getPixelsToDP(90));
                layoutParams.rightMargin = getPixelsToDP(10);
                frameLayout.setLayoutParams(layoutParams);

                /*--------------end of frame layout----------------------------*/

                /*---------------Creating image view----------------------*/
                final ImageView imgView = new ImageView(ActivityMap.this); //create imageview dynamically
                LinearLayout.LayoutParams lpImage = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                imgView.setImageBitmap(collectionInfo.get(v).getCatImage());
                imgView.setLayoutParams(lpImage);
                // setting ID to retrieve at later time (same as its position)
                imgView.setId(v);
                imgView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        long id = imgView.getId();
                        // getting id which is same as its position
                        Log.i(TAG, "Clicked on " + collectionInfo.get(v.getId()).getCatName());
                        // getting selected category's data list
                        new GetSelectedCategoryData().execute(collectionInfo.get(v.getId()).getCatID());
                    }
                });
                /*--------------end of image view----------------------------*/

                /*---------------Creating Text view----------------------*/
                TextView textView = new TextView(ActivityMap.this);//create textview dynamically
                textView.setText(collectionInfo.get(v).getCatName());
                FrameLayout.LayoutParams lpText = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM | Gravity.CENTER);
                // Note: LinearLayout.LayoutParams 's gravity was not working so I putted Framelayout as 3 paramater is gravity itself
                //lpText.gravity = Gravity.BOTTOM | Gravity.CENTER;
                textView.setTextColor(Color.parseColor("#43A047"));
                textView.setLayoutParams(lpText);
                /*--------------end of Text view----------------------------*/

                //Adding views at appropriate places
                frameLayout.addView(imgView);
                frameLayout.addView(textView);
                linearLayout.addView(frameLayout);

            }

the trick that is working here is the id that I have assigned to ImageView "imgView.setId(v)" and after that applying onClickListener to that I am again fetching the id of the view....

in this way I have successfully achieved the solution...I have developed this code few days back, I think I should share with everyone....Happy coding...

这篇关于如何有一个水平的ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 12:21