即時通訊開發——Fragment+ViewPager滑動主介面,fragmentviewpager

來源:互聯網
上載者:User

即時通訊開發——Fragment+ViewPager滑動主介面,fragmentviewpager

   今天開始想嘗試下一直以來很想要開發的即時通訊APP開發工作,但鑒於畢業設計的論文的提交日期漸漸臨近,外加上公司時不時給的工作任務,真心不知道自己能否堅持下去,但是想通過部落格刺激下自己吧,看是否能通過部落格的更新來加快或者調動自己開發的積極性。首先本人對這塊的知識點的瞭解真是少之又少,但是比較即時通訊是個大趨勢,這塊的技術真的不能丟,於是狠下心買下了一個課程,那就跟著課程掌握這塊知識點的開發工作吧,當然,介面會有很大不同,會根據個人對於介面的喜好,編寫自己的介面風格。所以,今天的介面風格的搭建也是純粹根據個人喜好編碼形成的,也沒什麼難度,有點略像網易新聞的滑動效果。

   大概技術實現:Fragment+ViewPager

   通過代碼實現起來不是很難,整體的布局列表為:


下面我會通過直接po上代碼的方式進行整體上的描述:

主介面代碼(MainActivity):

package com.jrue.instantchat;import java.util.ArrayList;import java.util.List;import com.jrue.instantchat.adapter.MyViewPagerAdapter;import com.jrue.instantchat.fragments.ChatFragment;import com.jrue.instantchat.fragments.ContactFragment;import com.jrue.instantchat.fragments.MoreFragment;import android.graphics.Color;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentActivity;import android.support.v4.view.ViewPager;import android.support.v4.view.ViewPager.OnPageChangeListener;import android.util.TypedValue;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends FragmentActivity implementsOnPageChangeListener, OnClickListener {private Button btn_chat, btn_contact, btn_more;private ViewPager mViewPager;private List<Fragment> mDatas;private MyViewPagerAdapter mAdapter;private int currentIndex;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();mAdapter = new MyViewPagerAdapter(getSupportFragmentManager(), mDatas);mViewPager.setAdapter(mAdapter);mViewPager.setOnPageChangeListener(this);}/** * 初始化控制項 */private void initView() {currentIndex = 0;mViewPager = (ViewPager) findViewById(R.id.vp_content);btn_chat = (Button) findViewById(R.id.btn_chat);btn_contact = (Button) findViewById(R.id.btn_contact);btn_more = (Button) findViewById(R.id.btn_more);btn_chat.setOnClickListener(this);btn_contact.setOnClickListener(this);btn_more.setOnClickListener(this);btn_chat.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 8, getResources().getDisplayMetrics()));btn_chat.setTextColor(Color.parseColor("#41AAF1"));mDatas = new ArrayList<Fragment>();mDatas.add(new ChatFragment());mDatas.add(new ContactFragment());mDatas.add(new MoreFragment());}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);return true;}/** * ViewPager滾動狀態改變時調用此方法 */@Overridepublic void onPageScrollStateChanged(int arg0) {}/** * ViewPager滾動時調用此方法 */@Overridepublic void onPageScrolled(int arg0, float arg1, int arg2) {}/** * 基本在此調用 */@Overridepublic void onPageSelected(int position) {resetButton();switch (position) {case 0:btn_chat.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 8, getResources().getDisplayMetrics()));btn_chat.setTextColor(Color.parseColor("#41AAF1"));break;case 1:btn_contact.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 8, getResources().getDisplayMetrics()));btn_contact.setTextColor(Color.parseColor("#41AAF1"));break;case 2:btn_more.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 8, getResources().getDisplayMetrics()));btn_more.setTextColor(Color.parseColor("#41AAF1"));break;}currentIndex = position;}/** * 對Button進行重設 */private void resetButton() {// 設定聊天按鈕為預設屬性btn_chat.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 6, getResources().getDisplayMetrics()));btn_chat.setTextColor(Color.parseColor("#969696"));// 設定連絡人按鈕為預設屬性btn_contact.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 6, getResources().getDisplayMetrics()));btn_contact.setTextColor(Color.parseColor("#969696"));// 設定更多按鈕為預設屬性btn_more.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 6, getResources().getDisplayMetrics()));btn_more.setTextColor(Color.parseColor("#969696"));}/** * 點擊事件處理 *  * @param v */@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btn_chat:currentIndex = 0;break;case R.id.btn_contact:currentIndex = 1;break;case R.id.btn_more:currentIndex = 2;break;}mViewPager.setCurrentItem(currentIndex);}}
注釋也比較詳細了,也不多講解了,而且代碼閱讀的難度上不是很高,很容易理解。


幾個Fragment的代碼:

 ChatFragment(每個Fragment的代碼基本相同,這裡只po上一個作為樣本):

package com.jrue.instantchat.fragments;import com.jrue.instantchat.R;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;/** * @author jruelee * */public class ChatFragment extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {return inflater.inflate(R.layout.chat_view,null);}@Overridepublic void onActivityCreated(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onActivityCreated(savedInstanceState);}}


當然少不過的布局檔案代碼:

這塊就更美難度了:

activity_main.xml:

<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:layout_height="45dp"        android:orientation="horizontal" >        <Button            android:id="@+id/btn_chat"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="1"            android:background="@null"            android:text="聊天"            android:textColor="@color/tab_bg" />        <Button            android:id="@+id/btn_contact"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="1"            android:background="@null"            android:text="連絡人"            android:textColor="@color/tab_bg"/>        <Button            android:id="@+id/btn_more"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="1"            android:background="@null"            android:text="更多"            android:textColor="@color/tab_bg"/>    </LinearLayout>    <android.support.v4.view.ViewPager        android:id="@+id/vp_content"        android:layout_width="match_parent"        android:layout_height="match_parent"/></LinearLayout>

chat_view/xml(同理,這幾個fragment布局也是相同的,只po上一個作為樣本):

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="聊天介面"        android:textSize="30sp"/></RelativeLayout>

最後順便奉上吧,挺喜歡這種效果的,而且自己真的很喜歡藍色,所以以前開發的應用色調全是藍色的 ==


如果需要源碼的話,晚點再上傳吧。



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.