On the logic control of the callback in the perfect interface of Android

Source: Internet
Author: User

Let Android into my life!

The company's recent project is particularly busy, it is difficult to take time to write a blog, but the more busy, contact the more things, learn more!

In this case, we would like to emphasize: just to cohesion, low-coupling idea of a simple realization, important ideas, not code, please pay attention to the vast number of readers friends!

The previous period of time the company's Android development on my one, thinking too closed, I want a what kind of, just write what kind, now, to two colleagues, the project by me and another colleague in charge, someone exchanges, can listen to other people's understanding of our improvement is absolutely beneficial harmless, we also need to adjust their mentality , to accept the correct advice from others!

Well, words don't say, go to the point, we please first look at the following picture, divided into the following sections

1: Enter the corresponding activity on the page, Red Standard 1 is RadioButton, Red Standard 2 is TextView, all control logic is implemented in the current activity

2: Red box, callout 3 part, is a imageselectfragment, corresponding to the activity of a framelayout layout, control it to show and hide

3:imageselectfragment displays all the pictures, with a GridView display, to the GridView configuration on the corresponding Gridadapter

4: There is a delete button in the top right corner of each picture, logic control is implemented in Gridadapter


So now the problem, we think, we write code, as far as possible to achieve its high portability, this may be the Android cohesion, low-coupling idea of the essence bar, let our code directly to others can use, then this imageselectfragment how to make high cohesion?

Here, we need to figure out what the imageselectfragment is going to accomplish, what it needs to do internally, what to communicate externally, and to understand that, we have the high cohesion goal.

The imageselectfragment to achieve is all the logical control of the image selection, whether you add, delete, the image URI data to other activity, exit the program and then come in to add or delete all the logic, this is what we want to achieve the goal, when the choice, When deleted, the number of Red label 2 must also change, that is, all your operations affect the data changes, be sure to notify the current activity.

Okay, guys, look at the implementation code for Imageselectfragment:



Import Java.io.File;
Import java.io.Serializable;
Import java.util.ArrayList;


Import Net.tsz.afinal.FinalBitmap;
Import Android.annotation.SuppressLint;
Import Android.content.Context;
Import android.content.Intent;
Import Android.net.Uri;
Import Android.os.Bundle;
Import android.os.Environment;
Import Android.provider.MediaStore;
Import android.support.annotation.Nullable;
Import Android.util.Log;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.view.inputmethod.InputMethodManager;
Import Android.widget.AdapterView;
Import Android.widget.AdapterView.OnItemClickListener;
Import Android.widget.GridView;


Import com.uhuibao.hupeng.BaseFragment;
Import COM.UHUIBAO.HUPENG.R;
Import Com.uhuibao.hupeng.adapter.GridAdapter;
Import Com.uhuibao.hupeng.adapter.GridAdapter.DeleteImageListener;
Import com.uhuibao.hupeng.config.Constants;
Import Com.uhuibao.hupeng.selectpic.BitmapUtils;
Import com.uhuibao.hupeng.selectpic.ImageGridActivity;
Import com.uhuibao.hupeng.selectpic.TestPicActivity;
Import Com.uhuibao.hupeng.utils.MToast;


/**
*
* @ClassName: Imageselectfragment
* @Description: TODO Logical control of picture selection
* @author Aricmiao
* @date 2014-12-16 9:28:59
*/
@SuppressLint ("Inflateparams")
public class Imageselectfragment extends Basefragment {


public static final int photohraph = 1;//photo
public static final int photofromlocal = 2; Take photos
public static final int photoodelete = 3;//Open Album
public static final int photolocaoresult = 4;//result
public static final String image_unspecified = "image/*";
/** in Tabhost's position * *
public static final int POSITION = 0;
Private view view;
Public GridView Gv_photo;
Private Gridadapter adapter;//put photos
Private Finalbitmap FRIENDSFB;
Private Context Mcontext;
Private arraylist<string> urls=new arraylist<string> ();
Private Imageselectlistener Mlistener;


@Override
Public View Oncreateview (Layoutinflater inflater,
@Nullable viewgroup container, @Nullable Bundle savedinstancestate) {
TODO auto-generated Method Stub
Mcontext=this.getactivity ();
View = Inflater.inflate (R.layout.image_select, NULL);
return view;
}


@Override
public void onactivitycreated (@Nullable Bundle savedinstancestate) {
TODO auto-generated Method Stub
Super.onactivitycreated (savedinstancestate);
FRIENDSFB = Finalbitmap.create (This.getactivity ());
Friendsfb.configdiskcachepath (Constants.picsavepath);
Friendsfb.configloadingimage (R.drawable.user);
Friendsfb.configloadfailimage (R.drawable.user);

Photo Container
Gv_photo = (GridView) View.findviewbyid (R.id.gv_photo);
adapter = new Gridadapter (this.getactivity (), FRIENDSFB);
Gv_photo.setadapter (adapter);
Adapter.notifydatasetchanged ();
Gv_photo.setonitemclicklistener (New Onitemclicklistener () {
public void Onitemclick (adapterview<?> arg0, View arg1, int arg2,
Long Arg3) {
LOG.D (Constants.tag,
"= = = Click location = = =" + arg2 + "= = = Picture Road Jin list===" + urls.size ());
if (arg2 = = Urls.size ()) {
Inputmethodmanager IMM = (inputmethodmanager) mcontext.getsystemservice (Context.input_method_service);
Imm.hidesoftinputfromwindow (Arg1.getwindowtoken (), 0);
New Popupwindows (ImageSelectFragment.this.getActivity (), Gv_photo,new Imagecallback () {

@Override
public void Onselectresult (String method) {
TODO auto-generated Method Stub
Intent intent=new Intent ();
if ("Camera". Equals (method)) {
String state = Environment.getexternalstoragestate ();
if (State.equals (environment.media_mounted)) {
Intent = new Intent (
Mediastore.action_image_capture);
Constants.camerafile = new File (Environment.getexternalstoragedirectory (),
String.valueof (System.currenttimemillis ()) + ". jpg");
LOG.D (Constants.tag, "= = = Photo Save Path = = =" + Constants.cameraFile.getPath ());
Uri Imageuri = Uri.fromfile (constants.camerafile);
Intent.putextra (Mediastore.extra_output, Imageuri);
Startactivityforresult (Intent, photohraph);
} else {
Mtoast.showtoast (Mcontext, "Wood has sd card oh");
}
}else if ("local". Equals (method)) {
Intent = new Intent (getactivity (),
Testpicactivity.class);
Startactivityforresult (Intent, photofromlocal);
}else if ("No". Equals (method)) {

}
}
});
}
}
});
Adapter.setdeletelistener (Deletelistener);
}

Deleteimagelistener deletelistener=new Deleteimagelistener () {

@Override
public void deleteimage (int position, String URL) {
TODO auto-generated Method Stub
Urls.remove (URL);
if (mlistener!=null) {
MLISTENER.IMAGESELCT (URLs);
}
}
};

public void Onactivityresult (int requestcode, int resultcode, Intent data) {
Switch (requestcode) {
Case photofromlocal://Local Selection
if (ResultCode = =-1) {
Return
}
if (constants.photofiles! = null) {
Intent Intent = new Intent (getactivity (),
Imagegridactivity.class);
Intent.putextra (Testpicactivity.extra_image_list,
(Serializable) Constants.photofiles);
Startactivityforresult (Intent, Photolocaoresult);
}
Break


Case photohraph://Photo
LOG.D (Constants.tag, "= = = Picture Count = = =" + BitmapUtils.drr.size ()
+ "= = = Result Code = = =" + ResultCode + "= = = Picture Path = = ="
+ Constants.cameraFile.getPath ());
if (BitmapUtils.drr.size () < Constants.photonum) {
if (Constants.cameraFile.exists ()) {
BitmapUtils.drr.add (Constants.cameraFile.getPath ());
}
}
Break


Case Photoodelete:
Break


Case Photolocaoresult:
Break
}
URLS=BITMAPUTILS.DRR;
if (mlistener!=null) {
MLISTENER.IMAGESELCT (URLs);
}
Super.onactivityresult (Requestcode, ResultCode, data);
}

@Override
public void Onresume () {
TODO auto-generated Method Stub
Adapter.notifydatasetchanged ();
Super.onresume ();
}

@Override
public void Ondestroyview () {
TODO auto-generated Method Stub
view = null;
Super.ondestroyview ();
}

public interface imageselectlistener{
public void Imageselct (arraylist<string> URLs);
}

public void Setimageselectlistener (Imageselectlistener listener) {
Mlistener=listener;
}
}



In fact, in the current imageselectfragment implementation of all operational control is not much to say, cohesion poly, low-coupling thought the main embodiment on the Imageselectlistener interface:


public interface imageselectlistener{
public void Imageselct (arraylist<string> URLs);
}

public void Setimageselectlistener (Imageselectlistener listener) {
Mlistener=listener;
}


We can implement this interface in our own activity.


Imageselectlistener mimageselectlistener=new Imageselectlistener () {

@Override
public void Imageselct (arraylist<string> URLs) {
TODO auto-generated Method Stub
Murls=urls;
Setimagetextnum ();
}
};


When manipulating the image data, any operation that causes the data to change will be called back to this method, the result of the return arraylist<string> URLs is the completion of the operation, the path of all the pictures, as long as you can get this path, We can arbitrarily set the activity in the display, delivery logic, and so, we do not care about what the user does to the picture, what the steps are, the impact of what these passes, if others want to transplant our pictures show fragment, It is also only necessary to implement this interface in his own activity, it is very convenient, so that our code in a completely closed environment to execute, only for the outside company an opening is enough, this pattern and many friends with the callback callback mechanism is basically the same.

Well, introduce here, if there are friends willing to communicate with each other, you can add qq:1531074759, thank you!

On the logic control of the callback in the perfect interface of Android

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.