Upload code
This commit is contained in:
125
source/src/me/caesar2011/vpherder/MainActivity.java
Normal file
125
source/src/me/caesar2011/vpherder/MainActivity.java
Normal file
@@ -0,0 +1,125 @@
|
||||
package me.caesar2011.vpherder;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import me.caesar2011.vpherder.teachersubstitution.TSCreate;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v4.app.FragmentPagerAdapter;
|
||||
import android.support.v4.view.ViewPager;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
public class MainActivity extends FragmentActivity {
|
||||
|
||||
/**
|
||||
* The {@link android.support.v4.view.PagerAdapter} that will provide
|
||||
* fragments for each of the sections. We use a
|
||||
* {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
|
||||
* will keep every loaded fragment in memory. If this becomes too memory
|
||||
* intensive, it may be best to switch to a
|
||||
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
|
||||
*/
|
||||
SectionsPagerAdapter mSectionsPagerAdapter;
|
||||
|
||||
/**
|
||||
* The {@link ViewPager} that will host the section contents.
|
||||
*/
|
||||
ViewPager mViewPager;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
// TSCreate the adapter that will return a fragment for each of the three
|
||||
// primary sections of the app.
|
||||
mSectionsPagerAdapter = new SectionsPagerAdapter(
|
||||
getSupportFragmentManager());
|
||||
|
||||
// Set up the ViewPager with the sections adapter.
|
||||
mViewPager = (ViewPager) findViewById(R.id.pager);
|
||||
mViewPager.setAdapter(mSectionsPagerAdapter);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
|
||||
* one of the sections/tabs/pages.
|
||||
*/
|
||||
public class SectionsPagerAdapter extends FragmentPagerAdapter {
|
||||
|
||||
public SectionsPagerAdapter(FragmentManager fm) {
|
||||
super(fm);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Fragment getItem(int position) {
|
||||
// getItem is called to instantiate the fragment for the given page.
|
||||
// Return a SectionFragment (defined as a static inner class
|
||||
// below) with the page number as its lone argument.
|
||||
Fragment fragment = new SectionFragment();
|
||||
Bundle args = new Bundle();
|
||||
args.putInt(SectionFragment.ARG_SECTION_NUMBER, position + 1);
|
||||
fragment.setArguments(args);
|
||||
return fragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
// Show 2 total pages.
|
||||
return 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getPageTitle(int position) {
|
||||
Locale l = Locale.getDefault();
|
||||
switch (position) {
|
||||
case 0:
|
||||
return getString(R.string.title_section1).toUpperCase(l); // teacher substitution today
|
||||
case 1:
|
||||
return getString(R.string.title_section2).toUpperCase(l); //settings
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A dummy fragment representing a section of the app, but that simply
|
||||
* displays dummy text.
|
||||
*/
|
||||
public static class SectionFragment extends Fragment {
|
||||
/**
|
||||
* The fragment argument representing the section number for this
|
||||
* fragment.
|
||||
*/
|
||||
public static final String ARG_SECTION_NUMBER = "section_number";
|
||||
|
||||
public SectionFragment() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
int page = getArguments().getInt(ARG_SECTION_NUMBER);
|
||||
if (page == 1) {
|
||||
return (new TSCreate(getActivity(), inflater, container)).Draw();
|
||||
}
|
||||
|
||||
// TODO delete this code
|
||||
View rootView = inflater.inflate(R.layout.fragment_main_dummy,
|
||||
container, false);
|
||||
TextView dummyTextView = (TextView) rootView
|
||||
.findViewById(R.id.section_label);
|
||||
dummyTextView.setText(Integer.toString(getArguments().getInt(
|
||||
ARG_SECTION_NUMBER)));
|
||||
return rootView;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package me.caesar2011.vpherder.teachersubstitution;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import me.caesar2011.vpherder.R;
|
||||
import android.content.Context;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.TextView;
|
||||
|
||||
/**
|
||||
* @author BastiLapTop
|
||||
*
|
||||
*/
|
||||
public class TSAdapter extends ArrayAdapter<TSjsonAnnouncement> {
|
||||
|
||||
private ArrayList<TSjsonAnnouncement> mData = new ArrayList<TSjsonAnnouncement>();
|
||||
private final LayoutInflater mInflater;
|
||||
|
||||
public TSAdapter(Context context, int textViewResourceId,
|
||||
ArrayList<TSjsonAnnouncement> objects) {
|
||||
super(context, textViewResourceId, objects);
|
||||
mData = objects;
|
||||
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return mData.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TSjsonAnnouncement getItem(int position) {
|
||||
return mData.get(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getItemId(int position) {
|
||||
return position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemViewType(int position) {
|
||||
// TODO Optimize
|
||||
if (mData.get(position) instanceof TSjsonAnnouncementRemark) {
|
||||
return AnnounceType.REMARK;
|
||||
} else {
|
||||
return AnnounceType.ALTERATION;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getViewTypeCount() {
|
||||
return AnnounceType.count;
|
||||
}
|
||||
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
ViewHolder holder = null;
|
||||
int type = getItemViewType(position);
|
||||
if (convertView == null) {
|
||||
switch (type) {
|
||||
case AnnounceType.REMARK:
|
||||
convertView = mInflater.inflate(R.layout.teacher_substitution_row_remark, null);
|
||||
break;
|
||||
case AnnounceType.ALTERATION:
|
||||
convertView = mInflater.inflate(R.layout.teacher_substitution_row_alteration, null);
|
||||
break;
|
||||
}
|
||||
holder = mData.get(position).createHolder(convertView);
|
||||
convertView.setTag(holder);
|
||||
} else {
|
||||
holder = (ViewHolder) convertView.getTag();
|
||||
}
|
||||
// (Re-)fill view with new data
|
||||
mData.get(position).refillView(holder);
|
||||
return convertView;
|
||||
}
|
||||
|
||||
|
||||
// SUB CLASSES
|
||||
|
||||
static class AnnounceType {
|
||||
public static final int REMARK = 0;
|
||||
public static final int ALTERATION = 1;
|
||||
public static final int count = 2;
|
||||
}
|
||||
|
||||
public static class ViewHolder {}
|
||||
|
||||
public static class ViewHolderREMARK extends ViewHolder {
|
||||
public TextView text;
|
||||
}
|
||||
|
||||
public static class ViewHolderALTERATION extends ViewHolder {
|
||||
public TextView teacher;
|
||||
public TextView subject;
|
||||
public TextView subject_subinfo;
|
||||
public TextView changes_from;
|
||||
public TextView changes_to;
|
||||
public TextView remark;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package me.caesar2011.vpherder.teachersubstitution;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.StringWriter;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import me.caesar2011.vpherder.R;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.json.JSONException;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.AsyncTask;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ListView;
|
||||
|
||||
/**
|
||||
* Class to organize the drawings of the teacher substition.
|
||||
*
|
||||
* @author BastiLapTop
|
||||
*
|
||||
*/
|
||||
public class TSCreate {
|
||||
|
||||
private final Activity activity;
|
||||
private final LayoutInflater inflater;
|
||||
private final ViewGroup container;
|
||||
private static TSjsonObject JsonObject;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public TSCreate(Activity activity, LayoutInflater inflater, ViewGroup container) {
|
||||
this.activity = activity;
|
||||
this.inflater = inflater;
|
||||
this.container = container;
|
||||
|
||||
if(JsonObject == null){
|
||||
try {
|
||||
URL url = new URL("https://vpherder.canis.uberspace.de/example.json");
|
||||
|
||||
DownloadFilesTask downloadSubstitutions = new DownloadFilesTask();
|
||||
downloadSubstitutions.execute(url);
|
||||
downloadSubstitutions.get();
|
||||
String theString = downloadSubstitutions.getOutput()[0];
|
||||
|
||||
JsonObject = new TSjsonObject(theString);
|
||||
} catch (ExecutionException e) {
|
||||
System.out.println("ERRRRRRRRRRRRRRRRRRRRROR ExecutionException!");
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("ERRRRRRRRRRRRRRRRRRRRROR InterruptedException!");
|
||||
} catch (JSONException e) {
|
||||
System.out.println("ERRRRRRRRRRRRRRRRRRRRROR JSONException!");
|
||||
} catch (MalformedURLException e) {
|
||||
System.out.println("ERRRRRRRRRRRRRRRRRRRRROR MalformedURLException!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public View Draw() {
|
||||
View rootView = inflater.inflate(R.layout.fragment_teacher_substitution,
|
||||
container, false);
|
||||
|
||||
final ListView listview = (ListView) rootView.findViewById(R.id.listview);
|
||||
|
||||
final TSAdapter adapter = new TSAdapter(activity,
|
||||
R.layout.teacher_substitution_row_remark, JsonObject.announcements);
|
||||
listview.setAdapter(adapter);
|
||||
|
||||
return rootView;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private class DownloadFilesTask extends AsyncTask<URL, Integer, String[]> {
|
||||
private String[] outputFiles;
|
||||
|
||||
protected String[] doInBackground(URL... urls) {
|
||||
int count = urls.length;
|
||||
String[] files = new String[count];
|
||||
for (int i = 0; i < count; i++) {
|
||||
URLConnection connection;
|
||||
try {
|
||||
connection = urls[i].openConnection();
|
||||
InputStream inputStream = connection.getInputStream();
|
||||
publishProgress((int) ((i / (float) count) * 100));
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
IOUtils.copy(inputStream, writer, "UTF-8");
|
||||
files[i] = writer.toString();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
|
||||
// Escape early if cancel() is called
|
||||
if (isCancelled()) break;
|
||||
}
|
||||
outputFiles = files;
|
||||
return files;
|
||||
}
|
||||
|
||||
protected void onProgressUpdate(Integer... progress) {
|
||||
//setProgressPercent(progress[0]);
|
||||
}
|
||||
|
||||
protected String[] getOutput() {
|
||||
return outputFiles;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package me.caesar2011.vpherder.teachersubstitution;
|
||||
|
||||
import me.caesar2011.vpherder.teachersubstitution.TSAdapter.ViewHolder;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.view.View;
|
||||
|
||||
public class TSjsonAnnouncement {
|
||||
|
||||
public final String type;
|
||||
|
||||
/**
|
||||
* @param jsonObject
|
||||
* @throws JSONException
|
||||
*/
|
||||
public TSjsonAnnouncement(JSONObject jsonObject) throws JSONException {
|
||||
// TODO Auto-generated constructor stub
|
||||
this.type = jsonObject.getString("type");
|
||||
}
|
||||
|
||||
public void refillView(ViewHolder holder) {
|
||||
|
||||
}
|
||||
|
||||
public ViewHolder createHolder(View convertView) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package me.caesar2011.vpherder.teachersubstitution;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
||||
import me.caesar2011.vpherder.R;
|
||||
import me.caesar2011.vpherder.teachersubstitution.TSAdapter.ViewHolder;
|
||||
import me.caesar2011.vpherder.teachersubstitution.TSAdapter.ViewHolderALTERATION;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
|
||||
/**
|
||||
* @author BastiLapTop
|
||||
*
|
||||
*/
|
||||
public class TSjsonAnnouncementAlteration extends TSjsonAnnouncement {
|
||||
|
||||
public final int time;
|
||||
public final String form;
|
||||
public final String teacher;
|
||||
public final String subject;
|
||||
public final boolean cancelled;
|
||||
public final ArrayList<TSjsonChange> changes;
|
||||
public final String remark;
|
||||
|
||||
/**
|
||||
* @param jsonObject
|
||||
* @throws JSONException
|
||||
*/
|
||||
public TSjsonAnnouncementAlteration(JSONObject jsonObject)
|
||||
throws JSONException {
|
||||
super(jsonObject);
|
||||
this.time = jsonObject.getInt("time");
|
||||
this.form = jsonObject.getString("form");
|
||||
this.teacher = jsonObject.getString("teacher");
|
||||
this.subject = jsonObject.getString("class");
|
||||
this.cancelled = jsonObject.getBoolean("cancelled");
|
||||
|
||||
this.changes = new ArrayList<TSjsonChange>();
|
||||
JSONObject jObject = jsonObject.getJSONObject("changes");
|
||||
Iterator<?> i = jObject.keys();
|
||||
|
||||
while(i.hasNext()){
|
||||
String key = (String)i.next();
|
||||
if(jObject.get(key) instanceof JSONObject){
|
||||
jObject.getJSONObject(key);
|
||||
TSjsonChange jChanges = new TSjsonChange(key,
|
||||
jObject.getJSONObject(key).getString("old"),
|
||||
jObject.getJSONObject(key).getString("new"));
|
||||
this.changes.add(jChanges);
|
||||
}
|
||||
}
|
||||
this.remark = jsonObject.optString("remark");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refillView(ViewHolder holder) {
|
||||
ViewHolderALTERATION holdAlteration = (ViewHolderALTERATION) holder;
|
||||
holdAlteration.teacher.setText(teacher);
|
||||
holdAlteration.subject.setText(subject);
|
||||
holdAlteration.subject_subinfo.setText(time + " - " + form);
|
||||
|
||||
String sFrom = "";
|
||||
String sTo = "";
|
||||
Iterator<TSjsonChange> i = changes.iterator();
|
||||
while (i.hasNext()) {
|
||||
TSjsonChange jChange = i.next();
|
||||
if (!sFrom.equals("")) {
|
||||
sFrom += "\n";
|
||||
sTo += "\n";
|
||||
}
|
||||
sFrom += jChange.oldValue;
|
||||
sTo += jChange.newValue;
|
||||
}
|
||||
holdAlteration.changes_from.setText(sFrom);
|
||||
holdAlteration.changes_to.setText(sTo);
|
||||
|
||||
if (remark.equals("false")) {
|
||||
holdAlteration.remark.setVisibility(View.GONE);
|
||||
} else {
|
||||
holdAlteration.remark.setText(remark);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ViewHolder createHolder(View convertView) {
|
||||
ViewHolderALTERATION holder = new ViewHolderALTERATION();
|
||||
holder.teacher = (TextView)convertView.findViewById(R.id.teacher);
|
||||
holder.subject = (TextView)convertView.findViewById(R.id.subject);
|
||||
holder.subject_subinfo = (TextView)convertView.findViewById(R.id.subject_subinfo);
|
||||
holder.changes_from = (TextView)convertView.findViewById(R.id.textview_from);
|
||||
holder.changes_to = (TextView)convertView.findViewById(R.id.textview_to);
|
||||
holder.remark = (TextView)convertView.findViewById(R.id.remark);
|
||||
return holder;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package me.caesar2011.vpherder.teachersubstitution;
|
||||
|
||||
import me.caesar2011.vpherder.R;
|
||||
import me.caesar2011.vpherder.teachersubstitution.TSAdapter.ViewHolder;
|
||||
import me.caesar2011.vpherder.teachersubstitution.TSAdapter.ViewHolderREMARK;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
|
||||
/**
|
||||
* @author BastiLapTop
|
||||
*
|
||||
*/
|
||||
public class TSjsonAnnouncementRemark extends TSjsonAnnouncement {
|
||||
|
||||
public final String text;
|
||||
|
||||
/**
|
||||
* @param jsonObject
|
||||
* @throws JSONException
|
||||
*/
|
||||
public TSjsonAnnouncementRemark(JSONObject jsonObject) throws JSONException {
|
||||
super(jsonObject);
|
||||
this.text = jsonObject.optString("text");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refillView(ViewHolder holder) {
|
||||
ViewHolderREMARK holdRemark = (ViewHolderREMARK) holder;
|
||||
holdRemark.text.setText(text);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ViewHolder createHolder(View convertView) {
|
||||
ViewHolderREMARK holder = new ViewHolderREMARK();
|
||||
holder.text = (TextView)convertView.findViewById(R.id.text);
|
||||
return holder;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package me.caesar2011.vpherder.teachersubstitution;
|
||||
|
||||
|
||||
/**
|
||||
* @author BastiLapTop
|
||||
*
|
||||
*/
|
||||
public class TSjsonChange {
|
||||
|
||||
public final String name;
|
||||
public final String oldValue;
|
||||
public final String newValue;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public TSjsonChange(String name, String oldValue, String newValue) {
|
||||
// TODO Auto-generated constructor stub
|
||||
this.name = name;
|
||||
this.oldValue = oldValue;
|
||||
this.newValue = newValue;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package me.caesar2011.vpherder.teachersubstitution;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
/**
|
||||
* The hole json object witch handles version, date, creation/modified time, etc. ...
|
||||
* And it holds a list of all announcements.
|
||||
*
|
||||
* @author BastiLapTop
|
||||
*
|
||||
*/
|
||||
public class TSjsonObject {
|
||||
|
||||
public final String version;
|
||||
public final Date date;
|
||||
public final Date created;
|
||||
public final Date modified;
|
||||
public final ArrayList<TSjsonAnnouncement> announcements;
|
||||
|
||||
|
||||
/**
|
||||
* @param jsonObject
|
||||
* @throws JSONException
|
||||
*/
|
||||
public TSjsonObject(JSONObject jsonObject) throws JSONException {
|
||||
// TODO Auto-generated constructor stub
|
||||
this.version = jsonObject.getString("version");
|
||||
|
||||
try {
|
||||
this.date = new SimpleDateFormat("yyyy-MM-dd", Locale.GERMANY).parse(jsonObject.getString("date"));
|
||||
} catch (ParseException e) {
|
||||
throw new JSONException("Date could not be converted. ParseExeption.");
|
||||
}
|
||||
|
||||
try {
|
||||
this.created = new Date(Long.parseLong(jsonObject.getString("created")) * 1000);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new JSONException("Created could not be converted. NumberFormatException.");
|
||||
}
|
||||
|
||||
try {
|
||||
this.modified = new Date(Long.parseLong(jsonObject.getString("modified")) * 1000);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new JSONException("Modified could not be converted. NumberFormatException.");
|
||||
}
|
||||
|
||||
JSONArray jArray = jsonObject.getJSONArray("announcements");
|
||||
JSONObject jObject = null;
|
||||
this.announcements = new ArrayList<TSjsonAnnouncement>();
|
||||
for (int i = 0; i < jArray.length(); i++) {
|
||||
jObject = jArray.getJSONObject(i);
|
||||
if (jObject.getString("type").equals("alteration")) {
|
||||
this.announcements.add(new TSjsonAnnouncementAlteration(jObject));
|
||||
} else {
|
||||
this.announcements.add(new TSjsonAnnouncementRemark(jObject));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public TSjsonObject(String jsonString) throws JSONException {
|
||||
this(new JSONObject(jsonString));
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
package me.caesar2011.vpherder.views;
|
||||
|
||||
import me.caesar2011.vpherder.R;
|
||||
import android.content.Context;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.widget.AbsListView;
|
||||
import android.widget.AbsListView.OnScrollListener;
|
||||
import android.widget.ListView;
|
||||
import android.widget.TextView;
|
||||
|
||||
public class PullToRefreshListView extends ListView implements OnScrollListener {
|
||||
|
||||
private float mStartX;
|
||||
private float mStartY;
|
||||
private int mRefreshState;
|
||||
private LayoutInflater mInflater;
|
||||
private View mHeaderView;
|
||||
private HeaderViewHolder mHeaderViewHolder;
|
||||
private int mLastTopVisiblePos;
|
||||
|
||||
private static final int PULL_DOWN_LIMIT = (int) Math.round(0.75 * DisplayMetrics.DENSITY_DEFAULT);
|
||||
|
||||
public PullToRefreshListView(Context context) {
|
||||
super(context);
|
||||
init(context);
|
||||
}
|
||||
|
||||
public PullToRefreshListView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
init(context);
|
||||
}
|
||||
|
||||
public PullToRefreshListView(Context context, AttributeSet attrs,
|
||||
int defStyle) {
|
||||
super(context, attrs, defStyle);
|
||||
init(context);
|
||||
}
|
||||
|
||||
public void init(Context context) {
|
||||
super.setOnScrollListener(this);
|
||||
|
||||
mInflater = (LayoutInflater) context.getSystemService(
|
||||
Context.LAYOUT_INFLATER_SERVICE);
|
||||
mHeaderView = mInflater.inflate(R.layout.pull_to_refresh_header, null);
|
||||
mHeaderViewHolder = new HeaderViewHolder(mHeaderView);
|
||||
addHeaderView(mHeaderView);
|
||||
|
||||
mRefreshState = RefreshState.PULL_TO_REFRESH;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onScrollStateChanged(AbsListView arg0, int arg1) {
|
||||
// TODO Auto-generated method stub
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onTouchEvent(MotionEvent event) {
|
||||
boolean isHandled = false;
|
||||
|
||||
if (event.getAction() == MotionEvent.ACTION_DOWN || mLastTopVisiblePos == 1) {
|
||||
int refreshBoxHeight = (int) ((mRefreshState == RefreshState.REFRESHING)?PULL_DOWN_LIMIT+15:1);
|
||||
mStartX = event.getRawX();
|
||||
mStartY = event.getRawY()-refreshBoxHeight;
|
||||
mHeaderViewHolder.setVisibility(View.VISIBLE);
|
||||
mHeaderView.setLayoutParams(new LayoutParams(mHeaderView.getLayoutParams().width, refreshBoxHeight));
|
||||
}
|
||||
if (event.getRawY()-mStartY > Math.abs(event.getRawX()-mStartX) && getFirstVisiblePosition() == 0) {
|
||||
int stretch = (int) Math.max(0, event.getRawY()-mStartY);
|
||||
System.out.println(event.getRawY()-mStartY);
|
||||
System.out.println(Math.abs(event.getRawX()-mStartX));
|
||||
System.out.println("--");
|
||||
mHeaderView.setLayoutParams(new LayoutParams(mHeaderView.getLayoutParams().width, stretch));
|
||||
if (mRefreshState == RefreshState.PULL_TO_REFRESH && stretch > PULL_DOWN_LIMIT) {
|
||||
mRefreshState = RefreshState.RELEASE_TO_REFRESH;
|
||||
mHeaderViewHolder.setRefreshingState(mRefreshState);
|
||||
} else if (mRefreshState == RefreshState.RELEASE_TO_REFRESH && stretch <= PULL_DOWN_LIMIT) {
|
||||
mRefreshState = RefreshState.PULL_TO_REFRESH;
|
||||
mHeaderViewHolder.setRefreshingState(mRefreshState);
|
||||
} else if (mRefreshState == RefreshState.ABORTING && stretch > PULL_DOWN_LIMIT) {
|
||||
mRefreshState = RefreshState.RELEASE_TO_REFRESH;
|
||||
mHeaderViewHolder.setRefreshingState(mRefreshState);
|
||||
} else if (mRefreshState == RefreshState.REFRESHING && stretch <= PULL_DOWN_LIMIT) {
|
||||
mRefreshState = RefreshState.ABORTING;
|
||||
mHeaderViewHolder.setRefreshingState(mRefreshState);
|
||||
}
|
||||
isHandled = true;
|
||||
}
|
||||
|
||||
if (event.getAction() == MotionEvent.ACTION_UP) {
|
||||
isHandled = false;
|
||||
switch (mRefreshState) {
|
||||
case RefreshState.PULL_TO_REFRESH:
|
||||
mHeaderViewHolder.setVisibility(View.GONE);
|
||||
mHeaderView.setLayoutParams(new LayoutParams(mHeaderView.getLayoutParams().width, 0));
|
||||
break;
|
||||
case RefreshState.RELEASE_TO_REFRESH:
|
||||
mHeaderViewHolder.setVisibility(View.VISIBLE);
|
||||
mRefreshState = RefreshState.REFRESHING;
|
||||
mHeaderViewHolder.setRefreshingState(mRefreshState);
|
||||
mHeaderView.setLayoutParams(new LayoutParams(mHeaderView.getLayoutParams().width, PULL_DOWN_LIMIT));
|
||||
break;
|
||||
case RefreshState.REFRESHING:
|
||||
mHeaderView.setLayoutParams(new LayoutParams(mHeaderView.getLayoutParams().width, PULL_DOWN_LIMIT));
|
||||
break;
|
||||
case RefreshState.ABORTING:
|
||||
mHeaderViewHolder.setVisibility(View.GONE);
|
||||
mRefreshState = RefreshState.PULL_TO_REFRESH;
|
||||
mHeaderViewHolder.setRefreshingState(mRefreshState);
|
||||
mHeaderView.setLayoutParams(new LayoutParams(mHeaderView.getLayoutParams().width, 0));
|
||||
break;
|
||||
}
|
||||
}
|
||||
mLastTopVisiblePos = getFirstVisiblePosition();
|
||||
return isHandled?true:super.onTouchEvent(event);
|
||||
}
|
||||
|
||||
private class HeaderViewHolder {
|
||||
public final View mParent;
|
||||
public final TextView mText;
|
||||
|
||||
public HeaderViewHolder(View HeaderView) {
|
||||
mParent = HeaderView;
|
||||
mText = (TextView) HeaderView.findViewById(R.id.text);
|
||||
}
|
||||
|
||||
public void setVisibility(int visibility) {
|
||||
mParent.setVisibility(visibility);
|
||||
mText.setVisibility(visibility);
|
||||
}
|
||||
|
||||
public void setRefreshingState(int refreshState) {
|
||||
switch (refreshState){
|
||||
case RefreshState.PULL_TO_REFRESH:
|
||||
mText.setText(R.string.pull_to_refresh_pull_label);
|
||||
break;
|
||||
case RefreshState.RELEASE_TO_REFRESH:
|
||||
mText.setText(R.string.pull_to_refresh_release_label);
|
||||
break;
|
||||
case RefreshState.REFRESHING:
|
||||
mText.setText(R.string.pull_to_refresh_refreshing_label);
|
||||
break;
|
||||
case RefreshState.ABORTING:
|
||||
mText.setText(R.string.pull_to_refresh_aborting_label);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class RefreshState {
|
||||
public static final int PULL_TO_REFRESH = 1;
|
||||
public static final int RELEASE_TO_REFRESH = 2;
|
||||
public static final int REFRESHING = 3;
|
||||
public static final int ABORTING = 4;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user