onClickListener不适用于ImageView在扩展PagerAdapter的Activity Adapter类中

时间:2015-04-13 07:41:15

标签: android android-viewpager onclicklistener android-pageradapter

onclicklistner在扩展PagerAdapter的ActivityAdapterClass中不起作用。任何人都可以帮助我做错了,因为我无法指出错误。我的代码如下: -

row_story_details.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">
<RelativeLayout 
        android:id="@+id/lay_ShareColumn_Story"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true">

        <ImageView
            android:id="@+id/iWhatsApp_Story_Story"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
                android:layout_marginRight="10dp"
                android:gravity="left|center"
                android:background="@drawable/whatsapp"
                android:layout_alignParentTop="true"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            />

        <ImageView
            android:id="@+id/iFaceBook_Story_Story"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentBottom="true"
            android:layout_toRightOf="@+id/iWhatsApp_Story_Story"
            android:background="@drawable/facebook"
            android:scaleType="centerCrop"
            android:clickable="true"
            android:layout_marginRight="10dp"/>

        <ImageView
            android:id="@+id/iGoogleShare"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentBottom="true"
            android:layout_toRightOf="@+id/iFaceBook_Story_Story"
            android:background="@drawable/google"
            android:scaleType="centerCrop"
            android:clickable="true"
            android:layout_marginRight="10dp"/>

        <ImageView
            android:id="@+id/iCommonShare"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentBottom="true"
            android:layout_toRightOf="@+id/iGoogleShare"
            android:background="@drawable/ic_action_share"
            android:scaleType="centerCrop"
            android:layout_marginRight="5dp"/>

        <ImageView
            android:id="@+id/iLastArtical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentBottom="true"
            android:layout_toLeftOf="@+id/iNextArtical"
            android:background="@drawable/ic_action_previous_item"
            android:scaleType="centerCrop"
            android:layout_marginRight="5dp"/>

        <ImageView
            android:id="@+id/iNextArtical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:background="@drawable/ic_action_next_item"
            android:scaleType="centerCrop"
            android:layout_marginRight="5dp"/>

        </RelativeLayout>

    <ScrollView 

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/ScrollView_StoryDetails2"
        android:layout_below="@+id/lay_ShareColumn"
        >

    <RelativeLayout 
        android:layout_marginTop="40dp"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">
    <TextView android:id="@+id/tTitle"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:textSize="18sp"
        android:typeface="sans"
        android:textStyle="bold"
        android:textColor="@color/Black"
        android:background="@color/White"
        android:layout_centerHorizontal="true"
        android:gravity="left"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_alignParentTop="true"/>
    <TextView android:id="@+id/tAuthorAndDateTime"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:typeface="sans"
        android:background="@color/White"
        android:textStyle="italic"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_width="match_parent"
        android:layout_below="@+id/tTitle"
        android:gravity="left"/>

    <WebView android:id="@+id/contentDetails"
        android:layout_width="match_parent"
        android:layout_below="@+id/tAuthorAndDateTime"
        android:layout_height="wrap_content"
        />

    </RelativeLayout>
    </ScrollView>
    </RelativeLayout>

StoryDe​​tailAdapter.java

package com.cricketcountry.ui.adapter;

import java.util.ArrayList;
import java.util.List;

import com.cricketcountry.R;
import com.cricketcountry.Bean.StoryBean;
import com.google.android.gms.plus.PlusShare;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Html;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class StoryDetailsAdapter extends PagerAdapter {

    Context context;
    ArrayList<StoryBean> liStory = new ArrayList<StoryBean>();
    WebView content;
    LayoutInflater inflater;
    String html2;
    int fontSize=3;
    String sTitle, sAuthor, sDateNTime, sContent, sLink, sContentWithoutVideo;
    String regExpIFrameCutter="\\<iframe.*?>.*?\\</iframe>";
    ImageView iGoogleShare, iFacebookShare, iCommonShare, iWhatsAppShare, ilastStory, iNextStory;
    public int position;
    TextView tTitle;
    TextView tAuthorNDateTime;
    ViewPager viewPager;
    boolean lastclick= false, currentlastwatched =false;


    public StoryDetailsAdapter(Context context, ArrayList<StoryBean> liStory, ViewPager viewPager) {
        this.context = context;
        this.liStory=liStory;
        this.viewPager=viewPager;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return liStory.size();
    }


    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        // TODO Auto-generated method stub
        return arg0 == ((RelativeLayout) arg1);
        //return arg0 == arg1;
    }



    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        // Declare Variables
        Log.i("Position instantiateItem:", position+"");
        this.position=position;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.row_story_details, container,
                false);

        // Locate the TextViews in viewpager_item.xml
        tTitle = (TextView) itemView.findViewById(R.id.tTitle);
        tAuthorNDateTime = (TextView) itemView.findViewById(R.id.tAuthorAndDateTime);
        iGoogleShare = (ImageView)itemView.findViewById(R.id.iGoogleShare);
        iFacebookShare = (ImageView)itemView.findViewById(R.id.iFaceBook_Story_Story); 
        iCommonShare = (ImageView)itemView.findViewById(R.id.iCommonShare);
        iWhatsAppShare = (ImageView)itemView.findViewById(R.id.iWhatsApp_Story_Story);
        ilastStory = (ImageView)itemView.findViewById(R.id.iLastArtical);
        iNextStory = (ImageView)itemView.findViewById(R.id.iNextArtical);
        content = (WebView) itemView.findViewById(R.id.contentDetails);

        content.getSettings().setPluginState(PluginState.ON_DEMAND);
        content.getSettings().setJavaScriptEnabled(true);
        content.getSettings().setAllowFileAccess(true);
        content.setWebChromeClient(new WebChromeClient());
        content.setWebViewClient(new MyAppWebViewClient());
        // Capture position and set to the TextViews
        sTitle = liStory.get(position).getStitle();
        sAuthor = liStory.get(position).getSauthor();
        sDateNTime = liStory.get(position).getSdate();
        sContent = liStory.get(position).getScontent();
        sLink = liStory.get(position).getSlink();

        tTitle.setText(Html.fromHtml(sTitle.toUpperCase()));
        tAuthorNDateTime.setText(Html.fromHtml("<font color=\"#0066FF\" face=\"arial, verdana, sans-serif\">"+liStory.get(position).getSauthor() +  "</font>" + "<font color=\"#D7D4D4\">" +" | " +/* df.format*/(liStory.get(position).getSdate().replace("+0000", ""))));
        //tTitle.setText(liStory.get(position).getStitle());
        //tAuthorNDateTime.setText(liStory.get(position).getSauthor());
        sContentWithoutVideo=(liStory.get(position).getScontent().replaceAll(regExpIFrameCutter, ""));

        html2 = 
                "<html><head>" + 
                    "</head>" +
                "<body>" +"<font size= "+fontSize+">"+
                        sContentWithoutVideo + "</font>"+
                "</body></html>";

                content.loadDataWithBaseURL("http://bgr.in/", html2, "text/html", "UTF-8", "");


            iGoogleShare.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Toast.makeText(context, "GoogleShare", Toast.LENGTH_LONG).show();
                    }
                });



            iFacebookShare.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Toast.makeText(context, "Facebook", Toast.LENGTH_LONG).show();
                    }
                });

            iWhatsAppShare.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    Toast.makeText(context, "Whatsapp", Toast.LENGTH_LONG).show();
                }
            });

            iCommonShare.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "CommonShare", Toast.LENGTH_LONG).show();
                }
            });

            ilastStory.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(context, "SomeLastStory", Toast.LENGTH_LONG).show();

                }
            });

            iNextStory.setOnClickListener(new View.OnClickListener() {
                @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                    Toast.makeText(context, "NextStroy", Toast.LENGTH_LONG).show();
                }
            });

        ((ViewPager) container).addView(itemView);

        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // Remove viewpager_item.xml from ViewPager
        ((ViewPager) container).removeView((RelativeLayout) object);

    }

}

2 个答案:

答案 0 :(得分:1)

在您拍摄的所有ImageView中添加以下行:

 android:focusable="false"
android:focusableInTouchMode="false"

并在OnClick事件中使其成为现实。这应该工作,因为我在我的一个应用程序中面临同样的问题,这解决了我的问题

答案 1 :(得分:0)

尝试将 descendantFocusability 属性添加到根RelativeLayout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:descendantFocusability="blocksDescendants">