如何在List上将ListView更改为GridView单击android

时间:2015-10-09 13:12:34

标签: android listview gridview

我想将我的视图列表视图更改为gridview,并在单击按钮时将gridview更改为listview。这是我的product.xml: -

@if(Members.GetCurrentLoginStatus().IsLoggedIn && Request.UrlReferrer != null) { 
    Response.Redirect(Request.UrlReferrer.AbsolutePath);
}

这是我的product_category.xml: -

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

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

   <ImageView
       android:id="@+id/image_product"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />

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

       <TextView
           android:id="@+id/product_id"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="name"
           android:visibility="gone"/>

      <TextView
          android:id="@+id/product_name"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="name"
          android:textColor="#166CED"
          android:textAppearance="?android:attr/textAppearanceLarge"/>

       <TextView
           android:id="@+id/product_price"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="1000"
           android:textColor="#D64530"/>

       <TextView
           android:id="@+id/product_discount"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="discout price"
           android:textColor="#D64530"
           android:textAppearance="?android:attr/textAppearanceSmall"/>
       <TextView
           android:id="@+id/product_href"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="name"
           android:visibility="gone"/>


       <RatingBar
           android:id="@+id/rating"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:visibility="gone"/>
   </LinearLayout>   </LinearLayout></LinearLayout>

这里是我的ProductCategory.java: -

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="shoppingmazza.android.catalyst.com.dynamiclayout.ProductCategory">

<!-- Header aligned to top -->

<RelativeLayout
    android:id="@+id/header_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true">

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="FILTER"
            android:id="@+id/textView" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="SORT"
            android:id="@+id/textView2"
            android:layout_marginLeft="257dp"
            android:layout_marginStart="257dp" />

    </LinearLayout>

</RelativeLayout>


 <LinearLayout
  android:layout_below="@+id/header_layout"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical">

  <ListView
      android:id="@+id/list_product"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      tools:listitem="@layout/product"/>

 </LinearLayout>
</RelativeLayout>

}

这里是我的ProductAdapter.java: -

public class ProductCategory extends AppCompatActivity {
private ListView listView;
ProductAdapter adapter;
ArrayList<shoppingmazza.android.catalyst.com.dynamiclayout.Product> productsList;
private int len;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.product_category);


    new Product().execute("http://opencart.codeniques.com/shopping/?route=feed/web_api/products&category=369&key=test123$");

    listView = (ListView)findViewById(R.id.list_product);
    productsList = new ArrayList<shoppingmazza.android.catalyst.com.dynamiclayout.Product>();
    adapter = new ProductAdapter(getApplicationContext(),R.layout.product,productsList);

    listView.setAdapter(adapter);

}
public class Product extends AsyncTask<String,Void,Boolean>{
    ProgressDialog dialog;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(ProductCategory.this);
        dialog.setMessage("Loading, please wait");
        dialog.setTitle("Connecting server");
        dialog.show();
        dialog.setCancelable(false);
    }

    @Override
    protected Boolean doInBackground(String... params) {
        try {
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(params[0]);
            HttpResponse response = client.execute(post);

            int status = response.getStatusLine().getStatusCode();
            if(status==200){
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);

                JSONObject jsonObject = new JSONObject(data);
                JSONArray jsonArray = jsonObject.getJSONArray("products");

                for(int i=0;i<jsonArray.length();i++){
                    len=jsonArray.length();
                    JSONObject jsonObject1 = jsonArray.getJSONObject(i);

                    shoppingmazza.android.catalyst.com.dynamiclayout.Product product = new shoppingmazza.android.catalyst.com.dynamiclayout.Product();


                    product.setId(jsonObject1.getString("id"));
                    product.setName(jsonObject1.getString("name"));
                    product.setPrice(jsonObject1.getString("pirce"));
                    product.setDiscountprice(jsonObject1.getString("discountprice"));
                    product.setHref(jsonObject1.getString("href"));
                    product.setThumb(jsonObject1.getString("thumb"));
                    product.setRating(jsonObject1.getString("rating"));

                    productsList.add(product);
                }
                return true;
            }

        }catch (IOException |JSONException e){
            Log.e("Error :",e.getMessage());
        }
        return null;
    }

    @Override
    protected void onPostExecute(Boolean aVoid) {
        dialog.dismiss();
        adapter.notifyDataSetChanged();
        if(!aVoid){
            Toast.makeText(ProductCategory.this, "Data is not Parsed", Toast.LENGTH_LONG).show();
        }
        else{
            ProductAdapter adapter = new ProductAdapter(getApplicationContext(),R.layout.product,productsList);
            listView.setAdapter(adapter);
        }

    }
}

我是android开发的新手请提前帮助我!

2 个答案:

答案 0 :(得分:4)

你可以添加ListView和GridView并设置GridView android:visibility =“gone”

 <LinearLayout
  android:layout_below="@+id/header_layout"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical">

  <ListView
      android:id="@+id/list_product"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      tools:listitem="@layout/product"/>

  <GridView
      android:id="@+id/list_productGV"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>


 </LinearLayout>
单击按钮上的

更改ListView的可见性,将GridView显示为可见,反之亦然

答案 1 :(得分:1)

1.您可以尝试仅使用GridView并使用gridview.setNumColumns(n);更改其列号。对于ListView模仿,只需使用n = 0

2.另一种选择是将RecyclerViewGridLayoutManager一起使用。

final int gridSpanCount = 4;
mImagesGrid = (RecyclerView) findViewById(R.id.gallery);
mLayoutManager = new GridLayoutManager(this, gridSpanCount);
mImagesGrid.setLayoutManager(mLayoutManager);
mSwitch = (Button) findViewById(R.id.switch_btn);
mSwitch.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v){
        if (mLayoutManager.getSpanCount() == gridSpanCount){
            mLayoutManager.setSpanCount(1);
        }
        else {
            mLayoutManager.setSpanCount(gridSpanCount);
        }
    }
}