xml的布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="wrap_content"> <android.support.design.widget.TabLayout android:id="@+id/tab" android:layout_width="wrap_content" android:layout_height="50dp"> </android.support.design.widget.TabLayout> <ImageView android:id="@+id/ma" android:src="@mipmap/jiahao" android:layout_width="20dp" android:layout_height="20dp" /> </LinearLayout> <android.support.v4.view.ViewPager android:id="@+id/viewpagets" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v4.view.ViewPager> </LinearLayout>package fengras.com.pindaoguanli; import android.content.Intent; import android.os.Bundle; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.GridView; import android.widget.ImageView; import java.util.ArrayList; import java.util.List; import channelmanager.bwie.com.draggridviewlibrary.AppApplication; import channelmanager.bwie.com.draggridviewlibrary.ChannelActivity; import channelmanager.bwie.com.draggridviewlibrary.bean.ChannelItem; import channelmanager.bwie.com.draggridviewlibrary.bean.ChannelManage; public class MainActivity extends AppCompatActivity { GridView gridView; Button bt; ArrayList<ChannelItem> userChannelList = new ArrayList<ChannelItem>(); private List<String> user = new ArrayList<>(); private List<String> other = new ArrayList<>(); private ImageView imageView; private TabLayout tabLayout; private ViewPager viewpager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView) findViewById(R.id.ma); tabLayout = (TabLayout) findViewById(R.id.tab); viewpager = (ViewPager) findViewById(R.id.viewpagets); user.add("推荐"); user.add("娱乐"); user.add("体育"); user.add("科技"); user.add("本地"); user.add("美女"); other.add("本地"); other.add("头条"); other.add("视频"); other.add("军事"); other.add("财经"); other.add("国际"); userChannelList = ((ArrayList<ChannelItem>) ChannelManage.getManage(AppApplication.getApp().getSQLHelper(), user, other).getUserChannel()); List<String> list = new ArrayList<>(); for (int i = 0; i < userChannelList.size(); i++) { list.add(userChannelList.get(i).getName()); Log.e("ssssssssssssssssssss", userChannelList.get(i).getName()); } FragmentPagerAdapter frag = Addapter(list); tabLayout.setTabMode(tabLayout.MODE_SCROLLABLE); viewpager.setAdapter(frag); tabLayout.setTabsFromPagerAdapter(frag);//数据传给Tab tabLayout.setupWithViewPager(viewpager); //关联ViewPager imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, ChannelActivity.class); intent.putStringArrayListExtra("user", (ArrayList<String>) user); intent.putStringArrayListExtra("other", (ArrayList<String>) other); startActivity(intent); } }); // gridView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list)); // bt.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View v) { // Intent intent = new Intent(MainActivity.this, ChannelActivity.class); // intent.putStringArrayListExtra("user", (ArrayList<String>) user); // intent.putStringArrayListExtra("other", (ArrayList<String>) other); // startActivity(intent); // } // }); } private FragmentPagerAdapter Addapter(final List<String> list ) { FragmentPagerAdapter fra = new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { Fragment01 fragment01 = new Fragment01(); Bundle bundle = new Bundle(); bundle.putString("str", list.get(position)); fragment01.setArguments(bundle); //将cattegory传到fragment中 return fragment01; } @Override public int getCount() { return list.size(); } @Override public CharSequence getPageTitle(int position) { return list.get(position); } }; return fra; } }
mainfest清单文件<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="fengras.com.pindaoguanli"> <!-- 在SDCard中创建与删除文件权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <!-- 往SDCard写入数据权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 震动权限 --> <uses-permission android:name="android.permission.VIBRATE" /> <application android:name="channelmanager.bwie.com.draggridviewlibrary.AppApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
频道管理的main方法
最新推荐文章于 2021-12-10 19:55:51 发布