• 组合练习各个功能


    public class ListViewAdapter extends BaseAdapter{
        private List<Bean> list;
        private Context context;
    
        public ListViewAdapter(List<Bean> list, Context context) {
            this.list = list;
            this.context = context;
        }
    
        @Override
        public int getCount() {
            return list.size();
        }
    
        @Override
        public Object getItem(int position) {
            return list.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(int position, View view, ViewGroup parent) {
            view = LayoutInflater.from(context).inflate(R.layout.layout,null);
            TextView text1 = view.findViewById(R.id.text1);
            TextView text2 = view.findViewById(R.id.text2);
            text1.setText(list.get(position).getTitle());
            text2.setText(list.get(position).getLikesCount()+"");
            return view;
        }
    }
    public class MainActivity extends AppCompatActivity {
        private ListView listView;
        private ListViewAdapter adapter;
    
    
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
           listView = findViewById(R.id.listview);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            toolbar.setTitle("首页");
            setSupportActionBar(toolbar);
            toolbar.setOnMenuItemClickListener(onMenuItemClickListener);
            
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setMessage("检测到新版本是否更新");
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(MainActivity.this, "更新完毕", Toast.LENGTH_SHORT).show();
                }
            }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(MainActivity.this, "还有更多新乐趣在等你哦", Toast.LENGTH_SHORT).show();
                }
            }).show();
    
    
            
            String url = "https://zhuanlan.zhihu.com//api/columns/growthhacker/posts?limit=10&offset=1";
            
            OkHttpUtils.get()
                    .url(url)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        
                        public void onError(Request request, Exception e) {
                            Toast.makeText(MainActivity.this, "网络连接失败", Toast.LENGTH_SHORT).show();
                        }
    
                        @Override
                        
                        public void onResponse(String response) {
                            
                            List<Bean> list = JSON.parseArray(response, Bean.class);
                            adapter = new ListViewAdapter(list,MainActivity.this);
                            listView.setAdapter(adapter);
                            Log.d("aaa", list.toString());
    
    
                        }
                    });
            
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
                int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    Window window = getWindow();
                    WindowManager.LayoutParams attributes = window.getAttributes();
                    attributes.flags |= flagTranslucentNavigation;
                    window.setAttributes(attributes);
                    getWindow().setStatusBarColor(Color.TRANSPARENT);
                } else {
                    Window window = getWindow();
                    WindowManager.LayoutParams attributes = window.getAttributes();
                    attributes.flags |= flagTranslucentStatus | flagTranslucentNavigation;
                    window.setAttributes(attributes);
                }
            }
    
    
    
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                
             getMenuInflater().inflate(R.menu.menu,menu);
            return true;
        }
    
        private Toolbar.OnMenuItemClickListener onMenuItemClickListener = new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()){
                    case R.id.action_menu1:
                        Intent intent = new Intent(MainActivity.this,Main2Activity.class);
                        startActivity(intent);
                        break;
                    case R.id.action_menu2:
                        Intent intent1 = new Intent(Intent.ACTION_VIEW);
                        intent1.setType("vnd.android-dir/mms-sms");
                        startActivity(intent1);
    
                        break;
                }
                return true;
            }
        };
    
    }
    public class Main2Activity extends AppCompatActivity {
        private WebView webView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
            webView = findViewById(R.id.webView);
            webView.loadUrl("https://www.jianshu.com/");
            webView.setWebViewClient(new WebViewClient(){
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }
            });
    
        }
    }
    <LinearLayout 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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.example.yu.shixuntext.MainActivity">
    
        <android.support.v7.widget.Toolbar
            android:paddingTop="20dp"
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#4e64df"
    
            >
    
    
    
        </android.support.v7.widget.Toolbar>
        <android.support.v4.widget.SwipeRefreshLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <ListView
                android:id="@+id/listview"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
            </ListView>
        </android.support.v4.widget.SwipeRefreshLayout>
    
    </LinearLayout>
    <RelativeLayout 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:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.yu.shixuntext.Main2Activity">
        <WebView
            android:id="@+id/webView"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
    
        </WebView>
    </RelativeLayout>

    子布局

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <TextView
            android:layout_marginTop="10dp"
            android:layout_marginLeft="10dp"
    
            android:text="热门回答"
            android:textSize="15dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:text="热门回答"
            android:id="@+id/text1"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="10dp"
            android:textSize="18dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
        <TextView
            android:id="@+id/text2"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="10dp"
            android:text="7"
            android:background="#2f62b4"
            android:textColor="#ffffff"
            android:paddingRight="15dp"
            android:paddingLeft="15dp"
            android:textSize="15dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
    
    </LinearLayout>

    在res中新建menu包创建menu布局

    <menu xmlns:android="http://schemas.android.com/apk/res/android">
    
            <item
                android:id="@+id/action_menu1"
                android:orderInCategory="300"
                android:menuCategory="container"
                android:title="关于"/>
            <item
                android:id="@+id/action_menu2"
                android:orderInCategory="200"
                android:menuCategory="system"
                android:title="分享"/>
    
    
    
    </menu>

    加入权限

    <uses-permission android:name="android.permission.SEND_SMS" />
        <uses-permission android:name="android.permission.INTERNET" />

    导入依赖

    compile 'com.jcodecraeer:xrecyclerview:1.3.2'
        compile 'com.squareup.okhttp3:okhttp:3.4.1'
        compile 'com.zhy:okhttputils:2.0.0'
        implementation 'com.google.code.gson:gson:2.2.4'
  • 相关阅读:
    mysql中bigint、int、mediumint、smallint 和 tinyint的取值范围
    centos6.5下安装samba服务器与配置
    centos 6.5 安装图形界面【转】
    Linux 下添加用户,修改权限
    Linux下自动调整时间和时区与Internet时间同步
    C#下利用封包、拆包原理解决Socket粘包、半包问题(新手篇)
    Unity脚步之NetworkBehaviour下前进、后退、左右转向的简单移动
    Token 在 Ajax 请求头中,服务端过滤器跨域问题
    【游戏】【暗黑2】重置属性点和技能点
    ASCII
  • 原文地址:https://www.cnblogs.com/SongYongQian/p/8758489.html
Copyright © 2020-2023  润新知