[英]Errors when trying to implement Swipe Views in android
Sorry for the code dump, I'm new to android and don't know where the problem is. 对不起,代码转储,我是android新手,不知道问题出在哪里。 I'm trying to implement swipe views in my android app and I'm having some trouble.
我试图在我的android应用中实现滑动视图,但遇到了一些麻烦。 I tried following this tutorial and this video but I'm getting some errors.
我尝试按照本教程和此视频进行操作,但出现一些错误。 I want the tabbed interface in my
MainActivity
我想要
MainActivity
的选项卡式界面
Here is my MainActivity.java
这是我的
MainActivity.java
package com.loomius.loomius;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
PagerAdapter pagerAdapter = new FixedTabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(pagerAdapter);
}
}
I'm getting this error for getSupporFragmentManager()
我在
getSupporFragmentManager()
遇到此错误
'FixedTabsPagerAdapter(android.app.FragmentManager)' in 'com.loomius.loomius.FixedTabsPagerAdapter' cannot be applied to '(android.support.v4.app.FragmentManager)'
and here is my FixedTabsPagerAdapter.java
这是我的
FixedTabsPagerAdapter.java
package com.loomius.loomius;
import android.app.FragmentManager;
import android.content.Context;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.app.Fragment;
import values.MatchesFragment;
import values.SuggestedSongsFragment;
import values.UserFragment;
public class FixedTabsPagerAdapter extends FragmentPagerAdapter{
public FixedTabsPagerAdapter (FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return 4;
}
@Override
public Fragment getItem(int position) {
switch(position) {
case 0:
return new SearchFragment();
case 1:
return new UserFragment();
case 2:
return new MatchesFragment();
case 3:
return new SuggestedSongsFragment();
default:
return null;
}
}
Context context;
@Override
public CharSequence getPageTitle (int position) {
switch(position) {
case 0:
return context.getResources().getString(R.string.search_frag_title);
case 1:
return context.getResources().getString(R.string.user_frag_title);
case 2:
return context.getResources().getString(R.string.matches_frag_title);
case 3:
return context.getResources().getString(R.string.sugg_frag_title);
default:
return null;
}
}
}
I'm getting this error for the return type Fragment
in the overridden method getItem
我在重写方法
getItem
遇到返回类型为Fragment
错误
'getItem(int)' in 'com.loomius.loomius.FixedTabsPagerAdapter' clashes with 'getItem(int)' in 'android.support.v13.app.FragmentPagerAdapter'; attempting to use incompatible return type
I put the android.support.v4.view.ViewPager
widget in my activity_main.xml
right below the android.support.v7.widget.Toolbar
widget. 我将
android.support.v4.view.ViewPager
小部件放在我的activity_main.xml
就在android.support.v7.widget.Toolbar
小部件的正下方。
Look at your FixedTabPagerAdapter constructor, you are trying to catch the reference of android.app.FragmentManager
instance while passing the fragment manager of type android.support.v4.app.FragmentManager
which are two different classes. 查看您的FixedTabPagerAdapter构造函数,您试图在传递
android.support.v4.app.FragmentManager
类型的片段管理器时捕获android.app.FragmentManager
实例的引用,这是两个不同的类。
Change the type of FragmentManager in your FixedTabPageAdapter to android.support.v4.app.FragmentManager
and it should fix the issue. 将FixedTabPageAdapter中的FragmentManager的类型更改为
android.support.v4.app.FragmentManager
,它应该可以解决此问题。
For sliding pages with tabs do following. 对于带有标签的滑动页面,请执行以下操作。
Download or copy following two files on github and paste your project. 在github上下载或复制以下两个文件,然后粘贴您的项目。 this is same as on developers.google.com except setDistributeEvenly method.
除setDistributeEvenly方法外,此方法与developers.google.com上的相同。
https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/widget/SlidingTabLayout.java https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/widget/SlidingTabLayout.java
https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/widget/SlidingTabStrip.java https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/widget/SlidingTabStrip.java
activity_main.xml activity_main.xml中
<your.package.name.SlidingTabLayout
android:clickable="true"
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</your.package.name.SlidingTabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
MyAdapter.java (Here i used two pages only) MyAdapter.java(在这里我只使用了两个页面)
class MyPagerAdapter extends FragmentPagerAdapter
{
String[] title = {"All","Favourites"};
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment=null;
if (position==0)
fragment= new All();
if (position==1)
fragment= new Favourites();
return fragment;
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
return title[position];
}
}
tab_view.xml (view of tab only , if you want u can also use ImageView here) tab_view.xml(仅选项卡的视图,如果您希望您也可以在此处使用ImageView)
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/tab_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text=""
android:padding="15dp"
android:textStyle="bold"
android:textSize="25dp"
/>
</FrameLayout>
MainActivity.java MainActivity.java
private SlidingTabLayout tabLayout;
private ViewPager pager;
tabLayout= (SlidingTabLayout) findViewById(R.id.tabs);
pager = (ViewPager) findViewById(R.id.pager);
tabLayout.setCustomTabView(R.layout.tab_view,R.id.tab_title);
MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
tabLayout.setDistributeEvenly(true);
tabLayout.setViewPager(pager);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.