使用ViewPager与ActionsContentView [英] Using ViewPager with ActionsContentView

查看:192
本文介绍了使用ViewPager与ActionsContentView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我使用 ActionsContentView
我的目标是在它实施ViewPager元素。利用只是一个其他视图。有没有人在这个项目做到了这一点?作者说,它不容易做,看到我是如何还在Android的编程(iOS版从迁移)很新,我觉得我需要帮助。

我已经准备好ViewPager的文档,但也似乎不能让他们正确地连接起来。

和帮助将是AP preciated。

更新:12/23

所以我设法获取其中的大部分用这个教程演示程序工作<一个href=\"http://looksok.word$p$pss.com/2013/10/26/android-support-v4-viewpager-tutorial-including-source-$c$c/\" rel=\"nofollow\">http://looksok.word$p$pss.com/2013/10/26/android-support-v4-viewpager-tutorial-including-source-$c$c/,然而,当我点击链接抽屉里​​,他们没有在主视图更新。

当它在RelativeLayout的设置,但ViewPager被打破这一部分将在下面的点击响应,但在设置的LinearLayout时,ViewPager工作,但不会链接响应。

 &LT; LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID /内容
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直&GT;&LT; android.support.v4.view.ViewPager
    机器人:ID =@ + ID / myViewPager
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT/&GT;
&LT; / LinearLayout中&GT;

另一件事我可能会想,在我的MainActivity,它没有更新正确的看法。点击它,关闭抽屉,然后什么也没有发生。我的继承人MainActivity。

 包com.chris.myapp;进口com.chris.myapp.adapter.ActionsAdapter;进口com.chris.myapp.fragment.WebViewFragment;
进口com.chris.myapp.MyPagerAdapter;进口android.content.Intent;
进口android.net.Uri;
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.support.v4.app.FragmentActivity;
进口android.support.v4.app.FragmentManager;
进口android.support.v4.app.FragmentTransaction;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.ListView;
进口android.support.v4.view.ViewPager;进口com.chris.myapp.R;
进口shared.ui.actionscontentview.ActionsContentView;
导入android.widget *。公共类MainActivity扩展FragmentActivity {  私有静态最后弦乐STATE_URI =状态:URI;
  私有静态最后弦乐STATE_FRAGMENT_TAG =状态:fragment_tag  私人ActionsContentView viewActionsContentView;  私人乌里currentUri = WebViewFragment.WEBVIEWFRAGMENT_URI;
  私人字符串currentContentFragmentTag = NULL;  @覆盖
  保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.example);    最后MyPagerAdapter pageAdapter =新MyPagerAdapter(getSupportFragmentManager());
    最后ViewPager寻呼机=(ViewPager)findViewById(R.id.myViewPager);
    pager.setAdapter(pageAdapter);    viewActionsContentView =(ActionsContentView)findViewById(R.id.actionsContentView);
    viewActionsContentView.setSwipingType(ActionsContentView.SWIPING_EDGE);
    viewActionsContentView.setSpacingType(ActionsContentView.SPACING_ACTIONS_WIDTH);
    viewActionsContentView.setSpacingWidth(650);
    viewActionsContentView.setActionsSpacingWidth(0);    最终的ListView viewActionsList =(ListView控件)findViewById(R.id.actions);
    最后ActionsAdapter actionsAdapter =新ActionsAdapter(本);
    viewActionsList.setAdapter(actionsAdapter);
    viewActionsList.setOnItemClickListener(新AdapterView.OnItemClickListener(){      @覆盖
      公共无效onItemClick(适配器视图&LT;&GT;适配器,视图V,INT位置,
          长标志){
        最后乌里URI = actionsAdapter.getItem(位置);        如果(EffectsActivity.URI.equals(URI)){
          startActivity(新意图(getBaseContext(),EffectsActivity.class));
          返回;
        }        updateContent(URI);
        viewActionsContentView.showContent();
      }
    });    如果(savedInstanceState!= NULL){
      currentUri = Uri.parse(savedInstanceState.getString(STATE_URI));
      currentContentFragmentTag = savedInstanceState.getString(STATE_FRAGMENT_TAG);
    }    updateContent(currentUri);
  }  公共无效onActionsButtonClick(查看视图){
    如果(viewActionsContentView.isActionsShown())
      viewActionsContentView.showContent();
    其他
      viewActionsContentView.showActions();
  }  @覆盖
  保护无效的onSaveInstanceState(捆绑outState){
    outState.putString(STATE_URI,currentUri.toString());
    outState.putString(STATE_FRAGMENT_TAG,currentContentFragmentTag);    super.onSaveInstanceState(outState);
  }  公共无效updateContent(URI URI){
    最后片段片段;
    最后弦乐标签;    最后FragmentManager FM = getSupportFragmentManager();
    最后FragmentTransaction TR = fm.beginTransaction();
    如果(WebViewFragment.WEBVIEWFRAGMENT_URI.equals(URI)){
      TAG = WebViewFragment.TAG;
      最终碎片foundFragment = fm.findFragmentByTag(标签);
      如果(foundFragment!= NULL){
        片段= foundFragment;
      }其他{
        片段=新WebViewFragment();
      }
    }否则如果(URI!= NULL){
      TAG = WebViewFragment.TAG;
      最后WebViewFragment webViewFragment;
      最终碎片foundFragment = fm.findFragmentByTag(标签);
      如果(foundFragment!= NULL){
        片段= foundFragment;
        webViewFragment =(WebViewFragment)片段;
      }其他{
        webViewFragment =新WebViewFragment();
        片段= webViewFragment;
      }
      webViewFragment.setUrl(uri.toString());
    }其他{
      返回;
    }    如果(fragment.isAdded()){
      tr.show(片段);
    }其他{
      tr.replace(R.id.content,片段标签);
    }
    tr.commit();    currentUri = URI;
    currentContentFragmentTag =标签;
  }
    私有静态长back_ pressed;
    私人吐司面包;
    @覆盖
    公共无效onBack pressed(){
        最终碎片currentFragment = getSupportFragmentManager()findFragmentByTag(currentContentFragmentTag)。
        如果(currentFragment的instanceof WebViewFragment){
            最后WebViewFragment webFragment =(WebViewFragment)currentFragment;
            如果(webFragment.onBack pressed())
                返回;
        }        如果(back_ pressed + 2000&GT; System.currentTimeMillis的())
        {            //这里需要取消敬酒
            toast.cancel();            // code出境
            意向意图=新意图(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(意向);
            完();        }
        其他
        {
            //要求用户preSS后退按钮一次,关闭应用程序
            敬酒= Toast.makeText(getBaseContext(),preSS再次退出,Toast.LENGTH_SHORT);
            toast.show();
            viewActionsContentView.showContent();
        }
        back_ pressed = System.currentTimeMillis的();
    }
}


解决方案

感谢您使用ActionsContentView库。

要在 ViewPager 更新片段的内容,请尝试更换​​ updateContent 方法体与下一个code:

 公共无效updateContent(URI URI){
    如果(URI == NULL)
        返回;    最后WebViewFragment webViewFragment =(WebViewFragment)
            pageAdapter.getItem(MyPagerAdapter.WEBVIEW_FRAGMENT_POSITION);
    webViewFragment.setUrl(uri.toString());
    webViewFragment.reload();
    currentUri = URI;
}

我得到了Web视图后,为了将内容加载到 ViewPager 的主框架。

So I'm using ActionsContentView My goal is to implement a ViewPager element within it. Utilizing just one other view. Has anybody done this with this project? The author says it's not easily done, and seeing how I'm still very new in Android programming (migrated from iOS) I feel I need help.

I have ready the docs on ViewPager, but couldn't seem to make them link up properly.

And help would be appreciated.

UPDATE: 12/23

So I managed to get most of this working on the demo app using this tutorial http://looksok.wordpress.com/2013/10/26/android-support-v4-viewpager-tutorial-including-source-code/, however, when I click links in the drawer, they don't update in main view.

This one part below will respond to clicks when its set in RelativeLayout, but ViewPager is broken, but when set in Linearlayout, ViewPager works, but links wont respond.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<android.support.v4.view.ViewPager
    android:id="@+id/myViewPager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
</LinearLayout>

Another thing I could be thinking that in my MainActivity, it's not updating the proper view. It clicks, closes the drawer and then nothing happens. Heres my MainActivity.

package com.chris.myapp;

import com.chris.myapp.adapter.ActionsAdapter;

import com.chris.myapp.fragment.WebViewFragment;
import com.chris.myapp.MyPagerAdapter;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.support.v4.view.ViewPager;

import com.chris.myapp.R;
import shared.ui.actionscontentview.ActionsContentView;
import android.widget.*;

public class MainActivity extends FragmentActivity {

  private static final String STATE_URI = "state:uri";
  private static final String STATE_FRAGMENT_TAG = "state:fragment_tag";

  private ActionsContentView viewActionsContentView;

  private Uri currentUri = WebViewFragment.WEBVIEWFRAGMENT_URI;
  private String currentContentFragmentTag = null;

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

    final MyPagerAdapter pageAdapter = new MyPagerAdapter(getSupportFragmentManager());
    final ViewPager pager = (ViewPager)findViewById(R.id.myViewPager);
    pager.setAdapter(pageAdapter);

    viewActionsContentView = (ActionsContentView) findViewById(R.id.actionsContentView);
    viewActionsContentView.setSwipingType(ActionsContentView.SWIPING_EDGE);
    viewActionsContentView.setSpacingType(ActionsContentView.SPACING_ACTIONS_WIDTH);
    viewActionsContentView.setSpacingWidth(650);
    viewActionsContentView.setActionsSpacingWidth(0);



    final ListView viewActionsList = (ListView) findViewById(R.id.actions);
    final ActionsAdapter actionsAdapter = new ActionsAdapter(this);
    viewActionsList.setAdapter(actionsAdapter);
    viewActionsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> adapter, View v, int position,
          long flags) {
        final Uri uri = actionsAdapter.getItem(position);

        if (EffectsActivity.URI.equals(uri)) {
          startActivity(new Intent(getBaseContext(), EffectsActivity.class));
          return;
        }

        updateContent(uri);
        viewActionsContentView.showContent();
      }
    });

    if (savedInstanceState != null) {
      currentUri = Uri.parse(savedInstanceState.getString(STATE_URI));
      currentContentFragmentTag = savedInstanceState.getString(STATE_FRAGMENT_TAG);
    }

    updateContent(currentUri);
  }

  public void onActionsButtonClick(View view) {
    if (viewActionsContentView.isActionsShown())
      viewActionsContentView.showContent();
    else
      viewActionsContentView.showActions();
  }

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    outState.putString(STATE_URI, currentUri.toString());
    outState.putString(STATE_FRAGMENT_TAG, currentContentFragmentTag);

    super.onSaveInstanceState(outState);
  }

  public void updateContent(Uri uri) {
    final Fragment fragment;
    final String tag;

    final FragmentManager fm = getSupportFragmentManager();
    final FragmentTransaction tr = fm.beginTransaction();


    if (WebViewFragment.WEBVIEWFRAGMENT_URI.equals(uri)) {
      tag = WebViewFragment.TAG;
      final Fragment foundFragment = fm.findFragmentByTag(tag);
      if (foundFragment != null) {
        fragment = foundFragment;
      } else {
        fragment = new WebViewFragment();
      }
    } else if (uri != null) {
      tag = WebViewFragment.TAG;
      final WebViewFragment webViewFragment;
      final Fragment foundFragment = fm.findFragmentByTag(tag);
      if (foundFragment != null) {
        fragment = foundFragment;
        webViewFragment = (WebViewFragment) fragment;
      } else {
        webViewFragment = new WebViewFragment();
        fragment = webViewFragment;
      }
      webViewFragment.setUrl(uri.toString());
    } else {
      return;
    }

    if (fragment.isAdded()) {
      tr.show(fragment);
    } else {
      tr.replace(R.id.content, fragment, tag);
    }
    tr.commit();

    currentUri = uri;
    currentContentFragmentTag = tag;
  }


    private static long back_pressed;
    private Toast toast;
    @Override
    public void onBackPressed() {
        final Fragment currentFragment = getSupportFragmentManager().findFragmentByTag(currentContentFragmentTag);
        if (currentFragment instanceof WebViewFragment) {
            final WebViewFragment webFragment = (WebViewFragment) currentFragment;
            if (webFragment.onBackPressed())
                return;
        }

        if (back_pressed + 2000 > System.currentTimeMillis()) 
        {

            // need to cancel the toast here
            toast.cancel();  

            // code for exit
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            finish();

        }
        else 
        {
            // ask user to press back button one more time to close app 
            toast=  Toast.makeText(getBaseContext(), "Press again to exit", Toast.LENGTH_SHORT);
            toast.show();
            viewActionsContentView.showContent();
        }
        back_pressed = System.currentTimeMillis();
    }
}

解决方案

Thanks for using ActionsContentView library.

To update content of fragment at ViewPager, please try replace updateContent method body with next code:

public void updateContent(Uri uri) {
    if (uri == null)
        return;

    final WebViewFragment webViewFragment = (WebViewFragment)
            pageAdapter.getItem(MyPagerAdapter.WEBVIEW_FRAGMENT_POSITION);
    webViewFragment.setUrl(uri.toString());
    webViewFragment.reload();
    currentUri = uri;
}

I got web view to load content into main frame of ViewPager after that.

这篇关于使用ViewPager与ActionsContentView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆