Using ViewPager with ActionsContentView

2019-09-04 17:35发布

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

1条回答
老娘就宠你
2楼-- · 2019-09-04 17:39

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.

查看更多
登录 后发表回答