Android Studio 滑动到不同的视图

Posted

技术标签:

【中文标题】Android Studio 滑动到不同的视图【英文标题】:Android Studio Swipe to different views 【发布时间】:2016-03-12 13:32:52 【问题描述】:

我是使用 android 工作室的新手我目前观看了一些关于设置基本滑动视图的教程,该视图最多可生成 3 个复制我的代码中称为 page_fragment_layout.xml 的原始页面。我想更进一步,能够链接包含一系列内容的不同页面。在这种情况下,我希望能够通过滑动将我的 Activity_main.xml 和 page_fragment_layout.xml 链接在一起。到目前为止,我已经添加了我的代码,我将非常感谢任何输入。

MainActivity.java

package socialdeveloper.swipecard;

import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends FragmentActivity 
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    viewPager = (ViewPager)findViewById(R.id.view_pager);
SwipeAdapter swipeAdapter = new SwipeAdapter(getSupportFragmentManager());
    viewPager.setAdapter(swipeAdapter);


PageFragment.java

package socialdeveloper.swipecard;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


/**
* A simple @link Fragment subclass.
*/
public class PageFragment extends android.support.v4.app.Fragment 
TextView textView;

public PageFragment() 
    // Required empty public constructor



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) 
    // Inflate the layout for this fragment
    View view =     inflater.inflate(R.layout.page_fragment_layout,container,false);
    textView = (TextView)view.findViewById(R.id.TestText);

    Bundle bundle = getArguments();
    String message = Integer.toString(bundle.getInt("count"));
    textView.setText("This is the "+message+ "Swipe View Page...");

    return view;



SwipeAdapter.java

package socialdeveloper.swipecard;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

/**
* Created by Hadleigh on 07/12/2015.
 */
public class SwipeAdapter extends FragmentStatePagerAdapter 


public SwipeAdapter(FragmentManager fm) 
    super(fm);


@Override
public Fragment getItem(int position) 
    Fragment fragment = new PageFragment();
    Bundle bundle = new Bundle();
    int i = 0;
    bundle.putInt("count",i+1);
    fragment.setArguments(bundle);
    return fragment;


@Override
public int getCount() 
    return 3;


【问题讨论】:

【参考方案1】:

在你的 SwipeAdapter.java 中使用这个

 `   public Fragment getItem(int position) 
         Fragment fragment = null;//Creates a fragment variable to hold the fragment class i create and make it a null so i can catch the error

       if (position == 0) 
            fragment = new PUT_THE_TITLE_OF_YOUR_ACTIVITY_YOU_WANT_TO_USE();//Calls the fragment.
        

       if (position == 1) 
            fragment = new CALL_A_DIFFERENT_FRAGMENT_OR_ACTIVITY();//Calls the fragment;
        

       if (position == 2) 
            fragment = new CALL_A_DIFFERENT_FRAGMENT_OR_ACTIVITY();//Calls the fragment.
        
    return fragment;
`

getItem() 方法中调用其他片段或活动。

【讨论】:

以上是关于Android Studio 滑动到不同的视图的主要内容,如果未能解决你的问题,请参考以下文章

Android Studio - 选项卡式活动如何将片段重置为默认视图?

如何将布局拆分为 3 个不同的滑动视图

用于按钮的 Android 滑动抽屉

在弹出的 Android Studio 中添加视图寻呼机

错误记录Android Studio 编译报错 ( Module was compiled with an incompatible version of Kotlin. ) 2

错误记录Android Studio 编译报错 ( Module was compiled with an incompatible version of Kotlin. ) 2