从webview

时间:2016-02-03 12:23:31

标签: android xml

我正在使用两个布局。在第一个布局中,我制作了故事设计,如数据,标题,图像等,然后在第二个布局中包含第一个布局。在第二个布局中,我有admob和相关的故事列表。当我打开此页面显示所有内容,但点击广告或其他功能,并在回归同一页后,webview数据将丢失。

在Xperia z,Moto X等一些设备上发生了这个问题,但在三星和Xiomi这个问题上工作正常。

如何解决这个问题。还有XML代码。

    <LinearLayout 

             xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             xmlns:ads="http://schemas.android.com/apk/res-auto"
             android:background="@color/white"
             android:orientation="vertical">

         <LinearLayout
           android:id="@+id/contentDetailImageLayout"
           android:layout_width="wrap_content"
           android:layout_height="220dp" >

            <ImageView
            android:id="@+id/bannerImagecondetail_slider"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@drawable/default_logo_list" />
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:background="@color/white" >

        <TextView
            android:id="@+id/cat_title_slider"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:background="@color/red_header"
            android:paddingBottom="3dp"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:paddingTop="3dp"
            android:singleLine="true"
            android:text="@string/charcha_main"
            android:textColor="@color/white"
            android:textSize="18dp" />

        <ImageView
            android:id="@+id/increaseImage_slider"
            android:layout_width="40dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"
            android:paddingBottom="3dp"
            android:src="@drawable/text_increament" />

        <TextView
            android:id="@+id/updated_date_slider"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_toLeftOf="@+id/increaseImage_slider"
            android:paddingBottom="3dp"
            android:text="March 16,2015 10:55 IST"
            android:textColor="@color/grey" />

        <TextView
            android:id="@+id/updated_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="2dp"
            android:layout_marginTop="5dp"
            android:layout_toLeftOf="@+id/updated_date_slider"
            android:paddingBottom="3dp"
            android:text="@string/ID_STRING_UPDATED"
            android:textColor="@color/grey_dark"
            android:visibility="gone" />

        <ImageView
            android:id="@+id/updated_logo"
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:layout_marginRight="3dp"
            android:layout_marginTop="8dp"
            android:layout_toLeftOf="@+id/updated_title"
            android:src="@drawable/clock" />
    </RelativeLayout>

    <TextView
        android:id="@+id/storyTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:textColor="@color/black"
        android:textSize="@dimen/heading_font_size" />

   <WebView
        android:id="@+id/storyText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:lineSpacingExtra="3dp"
        android:scrollbars="none"
        android:visibility="gone"
        android:textColor="@color/black" />



        <WebView
            android:id="@+id/ads_webview_content"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
             android:layout_gravity="center_horizontal"
             android:layout_marginBottom="5dp"
             android:layout_marginTop="5dp"
             android:background="@color/white"
             android:visibility="gone" />
             <RelativeLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_weight="7.2">

             com.google.android.gms.ads.AdView
              android:id="@+id/adView"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_centerHorizontal="true"
              android:layout_alignParentBottom="true"
              android:layout_marginLeft="10dp"
              android:layout_marginRight="10dp"
              ads:adSize="BANNER"
              ads:adUnitId="@string/banner_ad_unit_id">
            </com.google.android.gms.ads.AdView>
         </RelativeLayout>
       </LinearLayout>  


 This is the java code



    public Object instantiateItem(ViewGroup container, final int position) {
        ViewHolderItem viewHolder = null;

       inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemView = inflater.inflate(R.layout.content_detail_sliding_row, container, false);
    viewHolder = new ViewHolderItem();



    // Locate the TextViews in viewpager_item.xml
    viewHolder.title = (TextView) itemView.findViewById(R.id.storyTitle);
    viewHolder.storyText = (WebView) itemView .findViewById(R.id.storyText);
    viewHolder.imgflag = (ImageView) itemView .findViewById(R.id.bannerImagecondetail_slider);
    viewHolder.updatedDate = (TextView) itemView .findViewById(R.id.updated_date_slider);
    viewHolder.cat_title = (TextView) itemView.findViewById(R.id.cat_title_slider);
    viewHolder.increaseText = (ImageView) itemView .findViewById(R.id.increaseImage_slider);

    /*if(position == list.size()-1){
        mRightArraw.setVisibility(View.INVISIBLE);
    }
    else{
        mRightArraw.setVisibility(View.VISIBLE);
    }*/

    String f_title = list.get(position).getGalleryTitle();
    String imageurl = list.get(position).getGalleryImage();
    String time = list.get(position).getGalleryTime();
    String f_story_text = list.get(position).getGalleryBody();

    Controller.getInstance().Eventtracker("PhotoGallery swipe" ,"slide", f_title );
    // set to the TextViews
    if(f_title != null){
        try
        {
            f_title = new String(f_title.getBytes(), "UTF-8");
        }
        catch (UnsupportedEncodingException e)
        {
            Log.e("utf8", "conversion", e);
        }

        viewHolder.title .setText(f_title);
    }

    WebSettings webSettings = viewHolder.storyText.getSettings();
    webSettings.setJavaScriptEnabled(true);
    if (f_story_text != null){
        f_story_text = f_story_text.replaceAll("<b>", "");
        Spannable sp = new SpannableString(Html.fromHtml(f_story_text));
        Linkify.addLinks(sp, Linkify.ALL);
        final String html = "<body>" + Html.toHtml(sp) + "</body>";
        viewHolder.storyText.loadData(html, "text/html", "utf-8");
        viewHolder.storyText.setVisibility(View.VISIBLE);
    }

    SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = null;
    try {
        if (time != null)
            date = form.parse(time);
    } catch (java.text.ParseException e) {
        e.printStackTrace();
    }
    // set the time in ago formate.
    long timeInMilliseconds = date.getTime();
    String mFtime = Utility.calculateRelativeTime(timeInMilliseconds);
    /*if(mFtime.contains("in ")){
        mFtime = mFtime.replace("in", "from");

    }*/


    long currentTime= System.currentTimeMillis();
    if(timeInMilliseconds > currentTime){
        viewHolder.updatedDate.setText("0 min ago");
    }
    else{


     if(mFtime.contains("minut")){
        mFtime = mFtime.replace("minutes", "min");
        if(mFtime.contains("minute")){
            mFtime = mFtime.replace("minute", "min");
        }
    }
    if(mFtime.contains("hour")){
        mFtime = mFtime.replace("hours", "hrs");
        if(mFtime.contains("hour")){
            mFtime = mFtime.replace("hour", "hrs");
        }
    }

    /*PrettyTime p = new PrettyTime();
    String mConvertedtime = p.format(date);*/
    viewHolder.updatedDate.setText(mFtime);
    }
    // Capture position and set to the ImageView
    if (imageurl != null && !imageurl.isEmpty()) {
        try {
            imageLoader.displayImage(imageurl, viewHolder.imgflag);
            viewHolder.imgflag.setScaleType(ScaleType.FIT_XY);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    else{
        String uri = "@drawable/default_logo_list";

        int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName());

        Drawable res = context.getResources().getDrawable(imageResource);
        viewHolder.imgflag.setImageDrawable(res);
    }

    if (mCategoryName != null)
        viewHolder.cat_title.setText(mCategoryName);

    // Add viewpager_item.xml to ViewPager
    ((ViewPager) container).addView(itemView);

    viewHolder.increaseText.setVisibility(View.INVISIBLE);

    return itemView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    ((ViewPager) container).removeView((LinearLayout) object);

}

0 个答案:

没有答案