将ViewPager与ActionsContentView一起使用



所以我使用ActionsContentView我的目标是在其中实现一个ViewPager元素。只使用另一个视图。有人在这个项目上做过这个吗?作者说这并不容易,看到我在安卓编程(从iOS迁移而来)方面还是个新手,我觉得我需要帮助。

我已经在ViewPager上准备好了文档,但似乎无法使它们正确连接。

我们将不胜感激。

更新:12/23

所以我使用本教程在演示应用程序上完成了大部分工作http://looksok.wordpress.com/2013/10/26/android-support-v4-viewpager-tutorial-including-source-code/然而,当我点击抽屉中的链接时,它们不会在主视图中更新。

下面的这一部分在RelativeLayout中设置时将响应点击,但ViewPager已损坏,但在Linearlayout中设置时,ViewPager可以工作,但链接不会响应。

<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>

另一件事我可能会想,在我的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();
    }
}

感谢您使用ActionsContentView库。

要更新ViewPager处的片段内容,请尝试用下一个代码替换updateContent方法体:

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;
}

之后,我获得了将内容加载到ViewPager主框架中的web视图。

相关内容

  • 没有找到相关文章

最新更新