侧滑点击事件
来源:互联网 发布:网络管理 编辑:程序博客网 时间:2024/06/04 18:23
<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/frame_layout" android:layout_width="match_parent" android:layout_height="match_parent"></FrameLayout> <RelativeLayout android:id="@+id/relative_layout" android:layout_width="200dp" android:layout_height="match_parent" android:layout_gravity="left" android:background="#ffffff"> <ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent"></ListView> </RelativeLayout></android.support.v4.widget.DrawerLayout>
------------------------------------------主界面
package fragment.bwie.com.rikao19;import android.support.v4.widget.DrawerLayout;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.FrameLayout;import android.widget.ListView;import android.widget.RelativeLayout;import java.util.ArrayList;import java.util.List;import fragment.bwie.com.rikao19.fragment.NewsFragment;public class MainActivity extends AppCompatActivity { private DrawerLayout drawerLayout; private FrameLayout frameLayout; private RelativeLayout relativeLayout; private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);//布置成员变量 drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); frameLayout = (FrameLayout) findViewById(R.id.frame_layout); relativeLayout = (RelativeLayout) findViewById(R.id.relative_layout); listView = (ListView) findViewById(R.id.list_view);//添加list数据 final List<String> list = new ArrayList<>(); list.add("新闻"); list.add("关注"); list.add("动态"); list.add("设置");//把list数据添加到listview条目里 ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1, list); listView.setAdapter(arrayAdapter);//listview条目点击事件 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {//点击 NewsFragment newsFragment = new NewsFragment(); Bundle bundle = new Bundle(); if (list.get(i).equals("新闻")) { bundle.putString("name", "1"); } else if (list.get(i).equals("关注")) { bundle.putString("name", "2"); } else if (list.get(i).equals("动态")) { bundle.putString("name", "3"); } else if (list.get(i).equals("设置")) { bundle.putString("name", "4"); } newsFragment.setArguments(bundle); getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, newsFragment).commit(); drawerLayout.closeDrawer(relativeLayout); } }); }}---------------------------------------------------布局---------------------------------------------------<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.handmark.pulltorefresh.library.PullToRefreshListView xmlns:ptr="http://schemas.android.com/apk/res-auto" android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/refresh_list_view" ptr:ptrDrawable="@drawable/default_ptr_flip" ptr:ptrAnimationStyle="flip" ptr:ptrHeaderBackground="#383838" ptr:ptrHeaderTextColor="#FFFFFF" > </com.handmark.pulltorefresh.library.PullToRefreshListView></LinearLayout>PullToRefreshListView界面
package fragment.bwie.com.rikao19.fragment;import android.os.AsyncTask;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ListView;import com.google.gson.Gson;import com.handmark.pulltorefresh.library.ILoadingLayout;import com.handmark.pulltorefresh.library.PullToRefreshBase;import com.handmark.pulltorefresh.library.PullToRefreshListView;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;import fragment.bwie.com.rikao19.R;import fragment.bwie.com.rikao19.adapter.MyAdapter;import fragment.bwie.com.rikao19.bean.Bean;/** * Created by CZ on 2017/9/20. */public class NewsFragment extends Fragment { private PullToRefreshListView listview; private ILoadingLayout startLabels; private ILoadingLayout endLabels; private int num = 1; private int name = 1; private List<Bean.DataBean> list = new ArrayList<>(); private MyAdapter myAdapter; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.new_layout, container, false); listview = (PullToRefreshListView) view.findViewById(R.id.refresh_list_view); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Bundle bundle = getArguments(); final String name = bundle.getString("name", "1"); Log.i("---------------",name); getData(name); listview.setMode(PullToRefreshBase.Mode.BOTH); startLabels = listview.getLoadingLayoutProxy(true, false); startLabels.setPullLabel("下拉刷新"); startLabels.setRefreshingLabel("正在刷新..."); startLabels.setReleaseLabel("放开刷新"); endLabels = listview.getLoadingLayoutProxy(false, true); endLabels.setPullLabel("上拉刷新"); endLabels.setRefreshingLabel("正在载入..."); endLabels.setReleaseLabel("放开刷新..."); listview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() { //下拉 @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { num = 1; getData(name); myAdapter.notifyDataSetChanged(); } //上拉 @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { num++; getData(name); myAdapter.notifyDataSetChanged(); } }); } private void getData(final String name) { AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void, Void, String>() { @Override protected String doInBackground(Void... voids) { String path = "http://api.expoon.com/AppNews/getNewsList/type/" + name + "/p/" + num; try { URL url = new URL(path); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.setConnectTimeout(5000); urlConnection.setReadTimeout(5000); int responseCode = urlConnection.getResponseCode(); if (responseCode == 200) { InputStream inputStream = urlConnection.getInputStream(); String json = streamTostring(inputStream, "utf-8"); return json; } } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Gson gson = new Gson(); Bean bean = gson.fromJson(s, Bean.class); list.addAll(bean.getData()); myAdapter = new MyAdapter(list, getActivity()); listview.setAdapter(myAdapter); Date date = new Date(System.currentTimeMillis()); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm"); startLabels.setLastUpdatedLabel("上次刷新时间:" + simpleDateFormat.format(date)); } }; asyncTask.execute(); } private String streamTostring(InputStream inputStream, String s) { try { InputStreamReader inputStreamReader = new InputStreamReader(inputStream, s); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); StringBuilder builder = new StringBuilder(); String string = null; while ((string = bufferedReader.readLine()) != null) { builder.append(string); } bufferedReader.close(); return builder.toString(); } catch (Exception e) { e.printStackTrace(); } return null; }}
阅读全文
0 0
- 侧滑点击事件
- DrawerLayout侧滑布局点击不响应事件
- SwipeDelMenuLayout 条目侧滑 条目点击事件无效处理
- 关于Android DrawerLayout的侧滑菜单的点击事件
- DrawerLayout侧滑布局点击不响应事件
- 点击事件
- 点击事件
- 点击事件
- 点击事件
- 点击事件
- 点击事件
- 点击事件
- 点击事件
- 点击事件
- canvas 修改光标形状 滑过事件 点击事件
- js移动客户端--触摸事件 模拟点击、滑屏事件
- echarts 取消图例上的点击事件和图表上鼠标滑过点击事件
- LinearLayout 点击事件 添加点击事件代码
- 前端跳槽面试(慕课网笔记)
- oracle之存储过程,临时表,游标示例
- 尤肖虎,中国3G、4G掌门人
- 数组中的逆序对
- JAVA 字节流和字符流度读写的区别
- 侧滑点击事件
- 笔迹
- sql之left join、right join、inner join的区别
- 【leetcode】查找插入位置(二分)
- Vue.js画家技巧
- 内外连接
- 鹰潭设50亿元移动物联网产业基金 今年将成为NB-IoT爆发元年
- 微信小程序wx.request接收服务器返回信息
- 操作系统-进程线程