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>
子佈局android
<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佈局web
<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>
加入權限api
<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'