单击图像视图时在imageView中打开一个URL

时间:2017-01-18 06:38:18

标签: java android

我正在使用Adapter和Fragment.Fragment包含图像视图.Inside图像视图单击listener。我正在尝试调用url。这是我的代码

public class TabFragment1 extends Fragment {
private List<Stores> storesList = new ArrayList<>();
private RecyclerView recyclerView;
private StoresAdapter mAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.tab_fragment_1, container, false);
    ImageView imageView = (ImageView) view.findViewById(R.id.google);
    imageView.setBackgroundResource(R.drawable.animation);
    AnimationDrawable frameAnimation = (AnimationDrawable) imageView.getBackground();
    frameAnimation.start();
    recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
    recyclerView.setNestedScrollingEnabled(true);
    mAdapter = new StoresAdapter(storesList);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(mAdapter);
    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String url="www.google.com";
            Intent google=new Intent(Intent.ACTION_VIEW);
            google.setData(Uri.parse(url));
            startActivity(google);
        }
    });
    prepareStoresData();
    return view;
}
private void prepareStoresData() {
    Stores stores;
    stores = new Stores("Flipcart", "9 Offers", "Get upto 50% Cashback");
    storesList.add(stores);
    stores = new Stores("Amazon", "9 Offers", "No Cashback");
    storesList.add(stores);
    stores = new Stores("Paytm", "2 Offers", "Get upto 50%Cashback");
    storesList.add(stores);
    stores = new Stores("SnapDeal", "3 Offers", "Get upto 10% Reward Points");
    storesList.add(stores);
    stores = new Stores("Airtel Online Recharge", "2 Offers", "Get upto 0% Cashback");
    storesList.add(stores);
    stores = new Stores("Freecharge", "No Offers", "Get upto 0% Cashback");
    storesList.add(stores);
    stores = new Stores("Ebay", "No Offers", "Get upto 7% Cashback");
    storesList.add(stores);
    stores = new Stores("Shopclues", "No Offers", "Get upto 6% Cashback");
    storesList.add(stores);
    stores = new Stores("Panuval Book Store", "No Offers", "Get upto 5% Cashback");
    storesList.add(stores);
    stores = new Stores("Jabong", "2 Offers", "Get upto 6% Cashback");
    storesList.add(stores);
    stores = new Stores("Redbus", "1 Offers", "No Cashback");
    storesList.add(stores);
    stores = new Stores("FirstCry", "1 Offers", "Get upto ₹20 Cashback");
    storesList.add(stores);
    mAdapter.notifyDataSetChanged();
}

}

我已经给了我的代码帮助我了。它是具有imageview的适配器,它应该实现url传递。

3 个答案:

答案 0 :(得分:2)

如果你想在imageview之后在浏览器中打开一个网址,请点击这里尝试这个是java文件。

    public class TestFragment extends Fragment {

        private RecyclerView recyclerView;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.testxml, container, false);
            ImageView imageView = (ImageView) view.findViewById(R.id.google);


            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse("https://www.google.com"));
                    getActivity().startActivity(i);
                }
            });

            return view;
        }


}

这是我的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">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/google"
    android:src="@drawable/register_kyc_icon_house"
    />

</LinearLayout>

这是正在运行的code.please check.hope这有用。:)

答案 1 :(得分:0)

无法理解调用网址的含义是什么? 如果你的意思是在imageview点击浏览器中打开网址 然后在clicklistener

中添加此代码
Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse(url));
MyActivity.startActivity(intent);

MyActivity是由您的片段

组成的活动的名称

答案 2 :(得分:0)

点击此链接

Sending an Intent to browser to open specific URL

 imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String url="https://www.google.com";
            Intent google=new Intent(Intent.ACTION_VIEW);
            google.setData(Uri.parse(url));
            startActivity(google);
        }
    });

网址应包含 - https或http

感谢All.Hope这对其他人有帮助。