Android学习-新闻客户端养成记(三)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/w1143408997/article/details/52541209


主界面的实现

前面已做好了核心布局文件,接下来要做的就是让客户端活起来,现在的任务就是实现一个侧滑菜单的功能, 

     

实现这个功能也并不难,使用V4包下的DrawerLayout 控件可以轻松解决这一问题 ,代码如下,

package eNews.activity;

import java.lang.ref.WeakReference;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.DrawerLayout.DrawerListener;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.connect.common.Constants;
import com.tencent.tauth.Tencent;
import eNews.app.R;
import eNews.common.HandlerWhat;
import eNews.fragments.MainFragment;
import eNews.fragments.MoreAboutFragment;
import eNews.fragments.PictureFragment;
import eNews.fragments.VideoFragment;
import eNews.fragments.WeatherFragment;
import eNews.thirdParty.TencentThirdParty;

/**
 * 
 * @author k 主界面
 */
public class MainWindows extends Activity implements OnClickListener {

	@SuppressWarnings("unused")
	private MainWindowsHandler mainWindowsHandler;
	private ImageView logo;
	private DrawerLayout drawerLayout;          //V4 包下的抽屉控件

	private LinearLayout menu_left;           //左侧菜单控件
	private LinearLayout menu_mainLayout;   <span style="white-space:pre">	</span>
	private LinearLayout menu_picLayout;      
	private LinearLayout menu_videoLayout;    

	private LinearLayout menu_weatherLayout;  
	private LinearLayout menu_moreLayout;     
	private LinearLayout menu_collectLayout;  

	public MainFragment mainFragment;          //项目里的碎片
	public PictureFragment pictureFragment;
	public VideoFragment videoFragment;
	public WeatherFragment weatherFragment;
	public MoreAboutFragment aboutFragment;

	private static ImageButton userImgBtn;
	private static TextView userName;
	private showCollectActivity showcollectActivityListener; //收藏

	private boolean isOpen;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainwindows);  //对应的布局文件
		initTencentInstance();                 //初始化腾讯第三方实例 
		init();                                
		initFragment(); //初始化碎片容器

	}

	private void initTencentInstance() {
		TencentThirdParty.getInstance(getApplicationContext())
				.getTencentInstance();

	}

	private void init() {

		mainWindowsHandler = new MainWindowsHandler(this);
		mainFragment = new MainFragment();
		videoFragment = new VideoFragment();
		weatherFragment = new WeatherFragment();
		pictureFragment = new PictureFragment();
		aboutFragment = new MoreAboutFragment();
		showcollectActivityListener = new showCollectActivity();

		drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);          //获取抽屉控件实例
		drawerLayout.setDrawerShadow(R.drawable.shadow, GravityCompat.START);   //设置抽屉打开时的阴影部分

		drawerLayout.setDrawerListener(new ActionBarDrawerToggleListener());   //为抽屉设置监听事件
		menu_left = (LinearLayout) findViewById(R.id.menuLayout);
		menu_left.setOnTouchListener(new OnTouchListener() {

			@SuppressLint("ClickableViewAccessibility")
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				return true;
			}
		});

		menu_mainLayout = (LinearLayout) findViewById(R.id.menuMain);
		menu_mainLayout.setOnClickListener(this);

		menu_picLayout = (LinearLayout) findViewById(R.id.menuPic);
		menu_picLayout.setOnClickListener(this);

		menu_videoLayout = (LinearLayout) findViewById(R.id.menuVideo);
		menu_videoLayout.setOnClickListener(this);

		menu_weatherLayout = (LinearLayout) findViewById(R.id.menuWeather);
		menu_weatherLayout.setOnClickListener(this);

		menu_moreLayout = (LinearLayout) findViewById(R.id.menuMore);
		menu_moreLayout.setOnClickListener(this);

		menu_collectLayout = (LinearLayout) findViewById(R.id.menuCollect);
		menu_collectLayout.setOnClickListener(showcollectActivityListener);

		userImgBtn = (ImageButton) findViewById(R.id.userImg);
		userImgBtn.setOnClickListener(new LoginBtnClick());

		userName = (TextView) findViewById(R.id.userName);
		userName.setOnClickListener(showcollectActivityListener);

	}

	private void initFragment() {
		FragmentManager fm = getFragmentManager();
		FragmentTransaction transaction = fm.beginTransaction();
		transaction.replace(R.id.frame_content, mainFragment);
		transaction.commit();

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		ActionBar actionBar = getActionBar();
		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		actionBar.setCustomView(R.layout.actionbar_layout);
		actionBar.setDisplayShowCustomEnabled(true);

		logo = (ImageView) actionBar.getCustomView().findViewById(
				R.id.actionbar_logo);

		logo.setOnClickListener(new OnClickListener() {    //为actionbar上的logo图标设置监听,以便操作抽屉的打开与关闭

			@Override
			public void onClick(View v) {

				if (!isOpen) {

					drawerLayout.openDrawer(Gravity.LEFT);  //打开左侧抽屉
					isOpen = true;

				} else {
					drawerLayout.closeDrawer(Gravity.LEFT);  //关闭左侧抽屉
					isOpen = false;

					logo.setTranslationX(0);
				}

			}
		});

	}

	@Override
	public void onClick(View v) {    //监听左侧菜单的按钮
		// TODO Auto-generated method stub

		FragmentManager fm = getFragmentManager();
		FragmentTransaction transaction = fm.beginTransaction();
		switch (v.getId()) {

		case R.id.menuMain:

			drawerLayout.closeDrawer(menu_left);

			if (mainFragment == null)
				mainFragment = new MainFragment();
			transaction.replace(R.id.frame_content, mainFragment);  //替换当前的碎片

			System.out.println("menuVideo");
			break;

		case R.id.menuPic:

			drawerLayout.closeDrawer(menu_left);

			if (pictureFragment == null)
				pictureFragment = new PictureFragment();

			transaction.replace(R.id.frame_content, pictureFragment);

			System.out.println("menuPic");
			break;

		case R.id.menuVideo:

			drawerLayout.closeDrawer(menu_left);

			if (videoFragment == null)
				videoFragment = new VideoFragment();
			transaction.replace(R.id.frame_content, videoFragment);

			System.out.println("menuVideo");
			break;
		case R.id.menuWeather:

			drawerLayout.closeDrawer(menu_left);
			if (weatherFragment == null)
				weatherFragment = new WeatherFragment();
			transaction.replace(R.id.frame_content, weatherFragment);
			System.out.println("menuWeather");
			break;
		case R.id.menuMore:

			drawerLayout.closeDrawer(menu_left);
			if (aboutFragment == null)
				aboutFragment = new MoreAboutFragment();
			transaction.replace(R.id.frame_content, aboutFragment);
			System.out.println("menuMore");
			break;

		default:
			if (mainFragment == null)
				mainFragment = new MainFragment();
			transaction.replace(R.id.frame_content, mainFragment);
			break;
		}
		transaction.commit();

	}

	class showCollectActivity implements OnClickListener {  

		@Override
		public void onClick(View v) {               <span style="font-family: Arial, Helvetica, sans-serif;">//当点击收藏按钮时触发</span>


			if (TencentThirdParty.getInstance(getApplicationContext())
					.checkIsLogged()) {
				Intent intent = new Intent(MainWindows.this,
						CollectActivity.class);
				startActivity(intent);
			} else {
				Toast.makeText(getApplicationContext(), "请点击头像登录",
						Toast.LENGTH_SHORT).show();
			}

		}

	}

	class LoginBtnClick implements OnClickListener {

		@Override
		public void onClick(View v) {

			login();

		}

	}

	public void showMainFragment() {         //显示主碎片
		FragmentManager fm = getFragmentManager();
		FragmentTransaction transaction = fm.beginTransaction();
		if (mainFragment == null)
			mainFragment = new MainFragment();
		transaction.replace(R.id.frame_content, mainFragment);
		transaction.commit();

		getActionBar().show();

	}

	class ActionBarDrawerToggleListener implements DrawerListener {   //抽屉监听事件

		@Override
		public void onDrawerClosed(View drawerView) {  //抽屉关闭
			// TODO Auto-generated method stub

			System.out.println("onDrawerOpen ->" + isOpen + "");

			logo.setImageResource(R.drawable.menu_close);

		}

		@Override
		public void onDrawerOpened(View drawerView) {    //抽屉打开
			// TODO Auto-generated method stub

			System.out.println("onDrawerClosed ->" + isOpen + "");
			logo.setImageResource(R.drawable.menu_open);

		}

		@Override
		public void onDrawerSlide(View drawerView, float offset) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onDrawerStateChanged(int newState) {
			// TODO Auto-generated method stub

		}

	}

	static class MainWindowsHandler extends Handler {
		WeakReference<MainWindows> mainReference;  //设成弱引用,避免引起内存泄漏

		public MainWindowsHandler(MainWindows mainWindows) {
			// TODO Auto-generated constructor stub
			mainReference = new WeakReference<MainWindows>(mainWindows);

		}

		@Override
		public void handleMessage(Message msg) {  //handler 暂时没有用上
			// TODO Auto-generated method stub
			super.handleMessage(msg);

			MainWindows mainWindows = mainReference.get();

			if (mainWindows != null) {
				System.out.println("mainReference.get()");

				switch (msg.what) {

				case HandlerWhat.mainNews:
					break;

				case HandlerWhat.pictureNews:
					break;

				case HandlerWhat.videoNews:
					break;

				case HandlerWhat.weatherNews:
					break;

				default:
					break;
				}

			}

		}
	}

	private void login() {  //腾讯第三方登录

		TencentThirdParty.getInstance(getApplicationContext()).userLogin(this);
		// updateUserInfo();

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub

		if (requestCode == Constants.REQUEST_LOGIN
				|| requestCode == Constants.REQUEST_APPBAR) {    //腾讯官方文档说这行这行代码是为了照顾低端机,具体为什么,没有考究
			Tencent.onActivityResultData(
					requestCode,
					resultCode,
					data,
					TencentThirdParty.getInstance(getApplicationContext()).loginIUListener);
		}

		super.onActivityResult(requestCode, resultCode, data);
	}

	public static void setUserImgBtn(Bitmap bitmap) {   //设置用户头像

		if (bitmap == null)
			userImgBtn.setImageResource(R.drawable.people1);
		else
			userImgBtn.setImageBitmap(bitmap);

	}

	public static void setUserName(String name) {
		userName.setText(name);

	}

}
这样结合以前的布局文件,抽屉侧滑功能就可以实现了,写到这加上代码所提及到的碎片就可以看到及时的效果了,当然现在还不能看到新闻数据,因为还没有获取网络数据


源代码下载


展开阅读全文

没有更多推荐了,返回首页