GridView只顯示一行,可以橫著滑動
阿新 • • 發佈:2019-01-03
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">又是幾天沒有更新部落格了,前幾天搞了一下畢業設計,今天有點時間和大家分享下gridView橫著滑動的實現。我正在一點點的進步,如果現在有什麼做的不好的地方,歡迎批評指正。</span>
首先,還是先來點效果圖看看效果。因為我不知道怎麼把視訊放上來,所以貼幾張圖,把圖連貫起來看。
mainActivity程式碼:
activity_main.xml:import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.DisplayMetrics; import android.widget.GridView; import android.widget.HorizontalScrollView; import android.widget.LinearLayout; import com.android.volley.RequestQueue; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { GridView gridView; GridViewAdapter adapter; RequestQueue mRequestQueue; //橫屏滑動的gridview HorizontalScrollView horizontalScrollView; DisplayMetrics dm; private int NUM = 2; // 每行顯示個 private int LIEWIDTH;//每列寬度 private int LIE=4 ;//列數 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gridView = (GridView)findViewById(R.id.gridview); horizontalScrollView = (HorizontalScrollView) findViewById(R.id.scrollView); //horizontalScrollView.setHorizontalScrollBarEnabled(false);// 隱藏滾動條 //推薦商品 getScreenDen(); LIEWIDTH = dm.widthPixels / NUM; setValue(); adapter=new GridViewAdapter(this,LIE); } private void setValue() { adapter= new GridViewAdapter(this,LIE); //載入資料 List<DrugListInform> listData = new ArrayList<DrugListInform>(); for (int i = 0; i < 4; i++) { DrugListInform drugList = new DrugListInform(); drugList.setDrugName("測試"+i+"次"); listData.add(drugList); } adapter.setListData(listData); gridView.setAdapter(adapter); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(adapter.getCount() * LIEWIDTH, LinearLayout.LayoutParams.WRAP_CONTENT); gridView.setLayoutParams(params); gridView.setColumnWidth(dm.widthPixels /NUM); gridView.setStretchMode(GridView.NO_STRETCH); int count = adapter.getCount(); gridView.setNumColumns(count); } private void getScreenDen() { dm = new DisplayMetrics(); this.getWindowManager().getDefaultDisplay().getMetrics(dm); } }
GridViewAdapter:<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="wrap_content" android:layout_height="120dp"> <HorizontalScrollView android:id="@+id/scrollView" android:layout_width="wrap_content" android:layout_height="120dp" android:layout_weight="0.10" > <LinearLayout android:id="@+id/linearlayout" android:layout_width="wrap_content" android:layout_height="wrap_content" > <GridView android:id="@+id/gridview" android:layout_width="match_parent" android:layout_height="match_parent" > </GridView> </LinearLayout> </HorizontalScrollView> </LinearLayout> </RelativeLayout>
dtuglist_item:package com.example.grifviewheng; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import java.util.ArrayList; import java.util.List; /** * Created by Administrator on 2016/2/25 0025. */ public class GridViewAdapter extends BaseAdapter { private Context context; int count; private List<DrugListInform> listData=new ArrayList<DrugListInform>(); public GridViewAdapter(Context context,int count) { super(); this.context = context; this.count = count; } public List<DrugListInform> getListData() { return listData; } public void setListData(List<DrugListInform> listData) { this.listData = listData; } /*@Override public int getCount() { return listData.size(); }*/ @Override public int getCount() { return count; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View view, ViewGroup parent) { DrugListHolder holder=null; if(view==null){ holder=new DrugListHolder(); view=LayoutInflater.from(context).inflate(R.layout.druglist_item, null); holder.itemimage1 =(ImageView) view.findViewById(R.id.menu_image1_item); holder.itemName =(TextView) view.findViewById(R.id.drugName); holder.itemMoney= (TextView)view.findViewById(R.id.drugMoney); view.setTag(holder); }else{ holder=(DrugListHolder) view.getTag(); } DrugListInform drugListInform=listData.get(position); holder.itemName.setText(drugListInform.getDrugName()); return view; } //列表資料 public final class DrugListHolder { //列表圖片 ImageView itemimage1; //列表名 TextView itemName; //列表產家 TextView itemCompany; //價格 TextView itemMoney; } }
<?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">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:id="@+id/caidan_line">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="145dp"
android:background="#ffffff"
android:layout_marginLeft="6dp"
android:layout_marginRight="5dp"
android:id="@+id/caidan_relativeLayout">
<RelativeLayout
android:layout_marginTop="6dp"
android:id="@+id/caidan_relativeLayout_oneof"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<ImageView
android:src="@drawable/shoucang"
android:layout_marginTop="3dp"
android:id="@+id/menu_image1_item"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerHorizontal="true"/>
<TextView
android:text="測試一次"
android:id="@+id/drugName"
android:layout_below="@id/menu_image1_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_below="@id/drugName">
<ImageView
android:id="@+id/menu_image2_item"
android:layout_width="13dp"
android:layout_height="13dp"
android:src="@drawable/shopping_druglist_money"/>
<TextView
android:text="2.500"
android:id="@+id/drugMoney"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:layout_centerHorizontal="true"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
druglist_item可以換成自己需要的佈局。只要mainActivity中主體部分完善。
這樣就可以實現功能了,如果要網路請求資料,千萬不要忘記加網路許可權。