Android studio Swipe 的不同看法

发布时间: 2016/9/16 18:49:21
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我是新手使用 Android studio我目前看了几个教程上建立一个基本的 swipe view 以生成达 3 所复制原始页已知我作为 page_fragment_layout.xml 的代码中的一切。我想要走这一步能够链接包含某个范围的内容的不同页面。在这种情况下要能够联系我的 Activity_main.xml 和 page_fragment_layout.xml 在一起的 swipe 。我加入我的代码到目前为止,我将是最有鉴赏力的任何输入。

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()方法来调用其他fragments或activity。

官方微信
官方QQ群
31647020