2013-12-10 51 views
0

所以我使用ActionsContentView 我的目标是在其中实现一个ViewPager元素。利用另一种观点。有没有人用这个项目来做这件事?作者说这并不容易,并且看到我在Android编程方面仍然非常新颖(从iOS迁移),我觉得我需要帮助。使用ViewPager与ActionsContentView

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

和帮助,将不胜感激。

UPDATE: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中,它没有更新正确的视图。它点击,关闭抽屉,然后什么也没有发生。下面是我的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

感谢您使用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主框架。

+0

随着这个和更多的你的帮助一切工作太棒了。后续问题发布在这里。 http://stackoverflow.com/questions/21103886/implementing-jazzyviewpager – ChrisOSX