如何在Android Studio的自定义基本适配器网格视图中添加Web视图URL

时间:2019-06-21 13:09:30

标签: java android

I want to make an app where grid view will be there and when user click particular card he'll be redirected to particular website URL 
here I have used custom grid view with base adapter but how to redirect the user to particular site URL by onitemclicklistener 

我提供了图片附件,以了解制作此应用的实际目标

enter image description here

  

activity_main.xml

此代码实际上是为获取网格视图布局而编写的

       <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:background="#f8f8f8"
           android:orientation="vertical">

           <GridView
               android:id="@+id/customgrid"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:layout_below="@+id/os_texts"
               android:numColumns="2" />


           <TextView
               android:id="@+id/os_texts"
               android:layout_width="match_parent"
               android:layout_height="70dp"
               android:layout_marginBottom="10dp"
               android:layout_marginTop="2dp"
               android:background="#ffffff"
               android:gravity="center"
               android:text="All In One Shopping"
               android:textAlignment="center"
               android:textSize="25dp" />
       </RelativeLayout>
  

items.xml

在这里,我为网格视图的特定卡片外观创建了单独的布局

       <?xml version="1.0" encoding="utf-8"?>
       <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
           xmlns:app="http://schemas.android.com/apk/res-auto"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:padding="3dp">

           <android.support.v7.widget.CardView
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:clickable="true"
               android:elevation="9dp"
               android:foreground="?android:attr/selectableItemBackground"
               app:cardCornerRadius="5dp"
               app:cardUseCompatPadding="true">

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


                   <ImageView
                       android:id="@+id/os_images"
                       android:layout_width="match_parent"
                       android:layout_height="120dp"
                       android:scaleType="fitCenter"
                       android:src="@drawable/amazon" />

                   <View
                       android:layout_width="match_parent"
                       android:layout_height="1dp"
                       android:layout_marginLeft="10dp"
                       android:layout_marginRight="10dp"
                       android:background="#dadada">

                   </View>

                   <TextView
                       android:id="@+id/os_texts"
                       android:layout_width="match_parent"
                       android:layout_height="wrap_content"
                       android:layout_below="@+id/os_images"
                       android:layout_margin="10dp"
                       android:text="This is Just Dummy Text"
                       android:textAlignment="center"
                       android:textSize="18dp"
                       android:textStyle="bold" />

               </LinearLayout>

           </android.support.v7.widget.CardView>
       </RelativeLayout>
  

MainActivity.java

           package com.example.siraj.allinoneapp;
           import android.support.v7.app.AppCompatActivity;
           import android.os.Bundle;
           import android.view.View;
           import android.widget.AdapterView;
           import android.widget.GridView;


           public class MainActivity extends AppCompatActivity {

               public static Integer icons[] = {

                       R.drawable.amazon,
                       R.drawable.flipkart,
                       R.drawable.jabong,
                       R.drawable.myntra,
                       R.drawable.shopclues,
                       R.drawable.snapdeal

               };

               public static String sitenames[] = {"amazon", "flipkart", "jabong", "myntra", "shopclues", "snapdeal"};

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

                   final GridView gridView;
                   gridView = findViewById(R.id.customgrid);


                   gridView.setAdapter(new ImageAdapter(MainActivity.this, icons, sitenames));


                   gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                       @Override
                       public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                           {


                           }
                       }
                   });


               }
           };
  

ImageAdapter.java

            package com.example.siraj.allinoneapp;

           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;

           class ImageAdapter extends BaseAdapter {



           MainActivity mmainActivity;
               Integer[] micons;
               String[] msitenames;

               private static LayoutInflater inflater = null;

               public ImageAdapter(MainActivity mainActivity, Integer[] icons, String[] sitenames) {
                   msitenames = sitenames;
                   micons = icons;
                   mmainActivity = mainActivity;


                   inflater = (LayoutInflater) mainActivity.
                           getSystemService(Context.LAYOUT_INFLATER_SERVICE);
               }

               @Override
               public int getCount() {
                   return micons.length;
               }

               @Override
               public Object getItem(int position) {
                   return position;
               }

               @Override
               public long getItemId(int position) {
                   return position;
               }

               public class Holder {
                   ImageView os_img;
                   TextView os_txt;



  [1]: https://i.stack.imgur.com/9oojs.png

0 个答案:

没有答案