1. 程式人生 > >抽屜 fragment tablayout 輪播圖 顯示資料 點選跳轉

抽屜 fragment tablayout 輪播圖 顯示資料 點選跳轉

activity_main.xml佈局

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout xmlns:android=“http://schemas.android.com/apk/res/android” xmlns:app=“http://schemas.android.com/apk/res-auto” xmlns:tools=“http://schemas.android.com/tools” android:id="@+id/dl_root" android:layout_width=“match_parent” android:layout_height=“match_parent” tools:context=“com.example.lenovo.a06moni01.MainActivity”>

<RelativeLayout android:layout_width=“match_parent” android:layout_height=“wrap_content”

   >
   <LinearLayout
       android:id="@+id/bottom"
       android:layout_alignParentBottom="true"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal"
       >
       <TextView
           android:id="@+id/shouye"
           android:layout_width="0dp"
           android:layout_height="wrap_content"
           android:gravity="center"
           android:layout_weight="1"
           android:text="首頁"
           />
       <TextView
           android:id="@+id/redian"
           android:layout_width="0dp"
           android:gravity="center"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:text="熱點"
           />
       <TextView
           android:id="@+id/wode"
           android:layout_width="0dp"
           android:gravity="center"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:text="我的"
           />
   </LinearLayout>

   <android.support.v4.view.ViewPager
       android:id="@+id/vp"
       android:layout_above="@+id/bottom"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
     ></android.support.v4.view.ViewPager>

MainActivity頁面實現fragment

package com.example.lenovo.a06moni01;

import android.support.v4.app.Fragment; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView;

import com.example.lenovo.a06moni01.fragment.RedianFragment; import com.example.lenovo.a06moni01.fragment.ShouyeFragment; import com.example.lenovo.a06moni01.fragment.WodeFragment;

import java.util.ArrayList; import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private DrawerLayout dl_root; private ListView list_drawer; private List list; private TextView Shouye; private TextView Redian; private TextView Wode; private ViewPager Vp; private List listf; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dl_root=findViewById(R.id.dl_root); list_drawer=findViewById(R.id.list_drawer); Shouye=findViewById(R.id.shouye); Redian=findViewById(R.id.redian); Wode=findViewById(R.id.wode); Vp=findViewById(R.id.vp); //抽屜的資料 list=new ArrayList<>(); list.add(“我的會員”); list.add(“我的訊息”); list.add(“我的新聞”); //fragment的資料 listf=new ArrayList<>(); listf.add(new ShouyeFragment()); listf.add(new RedianFragment()); listf.add(new WodeFragment());

    Shouye.setOnClickListener(this);
    Redian.setOnClickListener(this);
    Wode.setOnClickListener(this);

    Vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
        @Override
        public Fragment getItem(int position) {
            return listf.get(position);
        }

        @Override
        public int getCount() {
            return listf.size();
        }
    });

   list_drawer.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,list));

}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.shouye:
            Vp.setCurrentItem(0);
            break;
        case R.id.redian:
            Vp.setCurrentItem(1);
            break;
        case R.id.wode:
            Vp.setCurrentItem(2);
            break;
    }
}

}

fragment_shouye.xml的佈局 實現tablayout 和輪播 顯示頁面

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.TabLayout android:id="@+id/tab_title" android:layout_width=“match_parent” android:layout_height=“wrap_content”></android.support.design.widget.TabLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/lunbo_vp"
    android:layout_width="400dp"
    android:layout_height="200dp"></android.support.v4.view.ViewPager>
<android.support.v4.view.ViewPager
   android:id="@+id/vp_news"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></android.support.v4.view.ViewPager>

shouyefragment實現tablayout 和輪播

package com.example.lenovo.a06moni01.fragment;

import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView;

import com.example.lenovo.a06moni01.R;

import java.util.ArrayList; import java.util.List;

/**

  • Created by lenovo on 2018-10-24. */

public class ShouyeFragment extends Fragment{ private TabLayout tab_Title; private ViewPager vp_News; private ViewPager lunbo_vp; private List imglist; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_shou, null, false); tab_Title= v.findViewById(R.id.tab_title); vp_News= v.findViewById(R.id.vp_news); lunbo_vp=v.findViewById(R.id.lunbo_vp); return v; }

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    final List<String> list=new ArrayList<>();
    list.add("娛樂");
    list.add("音樂");
    list.add("體育");
    list.add("新聞");
    list.add("美食");
    list.add("經濟");
    list.add("旅遊");
    List<String> url=new ArrayList<>();
    url.add("yu");
    url.add("yin");
    url.add("ti");
    url.add("xin");
    url.add("mei");
    url.add("jing");
    url.add("lv");

    final List<Fragment> fragments=new ArrayList<>();
    for (int i = 0; i <url.size() ; i++) {
       NewsFragment fragment=new NewsFragment();
       Bundle bundle=new Bundle();
       bundle.putString("url",url.get(i));

       fragment.setArguments(bundle);
       fragments.add(fragment);
    }
  tab_Title.setTabMode(TabLayout.MODE_SCROLLABLE);
    vp_News.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return list.get(position);
        }
    });


    tab_Title.setupWithViewPager(vp_News);

    imglist=new ArrayList<>();
    imglist.add(R.mipmap.lunb);
    imglist.add(R.mipmap.lunbb);
    imglist.add(R.mipmap.lunbbb);

    lunbo_vp.setAdapter(new PagerAdapter() {
        @Override
        public int getCount() {
            return imglist.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view==object;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            ImageView img=new ImageView(getActivity());
            img.setImageResource(imglist.get(position));
            container.addView(img);
            return img;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
           container.removeView((View) object);
        }
    });
    handler.sendEmptyMessageDelayed(1,2000);
}
Handler handler=new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        if (msg.what==1){
            int curren=lunbo_vp.getCurrentItem();
            if (curren<imglist.size()-1){
                curren++;
            }else{
                curren=0;
            }
            lunbo_vp.setCurrentItem(curren);
            sendEmptyMessageDelayed(1,2000);
        }

    }
};

}

實現顯示資料頁面的佈局

<?xml version="1.0" encoding="utf-8"?>

<com.bwie.xlistview.XListView android:id="@+id/list_view" android:layout_width=“match_parent” android:layout_height=“wrap_content”></com.bwie.xlistview.XListView>

NewsFragment頁面實現顯示資料程式碼 +點選事件

package com.example.lenovo.a06moni01.fragment;

import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ListView;

import com.bwie.xlistview.XListView; import com.example.lenovo.a06moni01.HttpUtils; import com.example.lenovo.a06moni01.News; import com.example.lenovo.a06moni01.NewsAdapter; import com.example.lenovo.a06moni01.R; import com.example.lenovo.a06moni01.ShowActivity; import com.google.gson.Gson;

import java.util.ArrayList; import java.util.List;

/**

  • Created by lenovo on 2018-10-24. */

public class NewsFragment extends Fragment{ public static final String url=“http://www.xieast.com/api/news/news.php?page=”; private int page=1; private String newsurl; private XListView listView; private List<News.DataBean> list; private NewsAdapter adapter; private Handler handler=new Handler(); @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v = inflater.inflate(R.layout.item_news, null, false); listView= v.findViewById(R.id.list_view); return v; }

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle bundle=getArguments();
    newsurl=bundle.getString("url");
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    list=new ArrayList<>();
    adapter=new NewsAdapter(getContext(),list);
    listView.setAdapter(adapter);
    listView.setPullRefreshEnable(true);
    listView.setPullLoadEnable(true);
     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
         @Override
         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
             Intent intent=new Intent(getContext(), ShowActivity.class);
             String sc=list.get(position).getUrl();
             intent.putExtra("sc",sc);
             startActivity(intent);

         }
     });
    listView.setXListViewListener(new XListView.IXListViewListener() {
        @Override
        public void onRefresh() {
            page=1;
            getdata(page,true);
        }

        @Override
        public void onLoadMore() {
         page++;
         getdata(page,false);
        }
    });
    getdata(page,false);
}

private void getdata(int page,final boolean b) {
    new AsyncTask<String, Integer, String>() {
        @Override
        protected String doInBackground(String... strings) {
            return HttpUtils.getInputHttp(strings[0]);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Gson gson=new Gson();
            News news=gson.fromJson(s,News.class);
            if (news!=null){
                List<News.DataBean> data=news.getData();
                if (data!=null){
                    if (!b){
                        list.clear();
                    }
                    list.addAll(data);
                    adapter.notifyDataSetChanged();
                }

            }
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                  if (b){
                      listView.stopLoadMore();
                  }else{
                      listView.stopRefresh();
                      listView.setRefreshTime("剛剛");
                  }
                }
            },2000);
        }
    }.execute(url+page);
}

}

點選過去的接受頁面佈局

<?xml version="1.0" encoding="utf-8"?>

ShowActivity接受頁面程式碼

package com.example.lenovo.a06moni01;

import android.content.Intent; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.webkit.WebResourceRequest; import android.webkit.WebView; import android.webkit.WebViewClient;

public class ShowActivity extends AppCompatActivity { private WebView wv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_show); wv=findViewById(R.id.wv); Intent intent=getIntent(); String con=intent.getStringExtra(“sc”); wv.loadUrl(con);

    wv.setWebViewClient(new WebViewClient(){

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            return super.shouldOverrideUrlLoading(view, request);
        }
    });
}

}

httputils

package com.example.lenovo.a06moni01;

import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL;

/**

  • Created by lenovo on 2018-10-24. */

public class HttpUtils { public static String getInputHttp(String urlString){ String result=""; try { URL url=new URL(urlString); HttpURLConnection connection= (HttpURLConnection) url.openConnection(); connection.setRequestMethod(“GET”); connection.setUseCaches(true); connection.setDoInput(true); connection.setDoOutput(false); connection.connect();

        if (connection.getResponseCode()==200){
            InputStream is=connection.getInputStream();
            result=getInputStream(is);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return  result;
}

private static String getInputStream(InputStream is) {
    String result="";
    try {
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        int lenght=-1;
        byte[] buffer=new byte[1024];
        while ((lenght=is.read(buffer,0,buffer.length))!=-1){
             baos.write(buffer,0,lenght);
             baos.flush();
        }
        result=baos.toString();
        baos.close();
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return  result;
}

}