问题
I have an activity, in which I have created a populated a table view with XML data parsed. The table view looks the way I want it and functionally it is also correct, but I want to enhance the vertical scrolling feature of the table view.
The Sr.No. column is vertically scrollable, but horizontally fixed. This is as per my requirement, hence no change is required!
Screen shot:
The whole table is vertically scrollable, including the serial no column. This is also as per my requirement, hence no change is required!
Screen shot:
My requirement is that, the header of the tables will remain vertically fixed while scrolling, such that the user will be able to see the the column names even if he scrolls down to the bottom of the table.
The screen shot of the desired output:
Source code of activity:
public class ReportListActivity extends Activity {
TableLayout srno_table;
TableRow srno_report_tr_data;
TableLayout report_table;
TableRow report_tr_data;
//variables to send post request
String reportList_responseBody;
String reportList_responseCode;
//variables to parse xml
String report_list_xmlContent;
// XML node keys of Exam Details
static final String KEY_SET = "Set"; // parent node
static final String KEY_EXAMSETID = "ExamSetId";
static final String KEY_SETID = "SetId";
static final String KEY_SETNAME = "SetName";
static final String KEY_FULLMARKS = "FullMarks";
static final String KEY_RIGHTMARKS = "RightMarks";
static final String KEY_WRONGMARKS = "WrongMarks";
static final String KEY_LEFTMARKS = "LeftMarks";
static final String KEY_OBTAINMARKS = "ObtainMarks";
static final String KEY_STUDENTRANK = "StudentRank";
static final String KEY_EXAMDATE = "ExamDate";
//Progressbar object
ProgressDialog progress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_LEFT_ICON);
setContentView(R.layout.activity_report_list);
//setting icon to title bar
getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.ic_launcher);
//starting the progress bar
progress = ProgressDialog.show(this, "Please Wait","Getting report list", true);
//===============For marquee===================================
TextView marqueeTextView = (TextView) findViewById(R.id.marquee_textView);
marqueeTextView.setText(Html.fromHtml(getFromPreference("marquee_Html_content")));
marqueeTextView.setSelected(true);
//===============For marquee===================================
//sending post request to download data
reportList_isOnline();
srno_table=(TableLayout) findViewById(R.id.srno_table);
//---------------Serial no Table Header-----------------------------------------------
TableRow srno_tr_head = new TableRow(this);
srno_tr_head.setId(10);
srno_tr_head.setBackgroundResource(R.drawable.list_header);
srno_tr_head.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView label_sr_no = new TextView(this);
label_sr_no.setId(20);
label_sr_no.setText("S.No.");
label_sr_no.setTextColor(Color.WHITE);
label_sr_no.setPadding(5,5,5,5);
srno_tr_head.addView(label_sr_no);// add the column to the table row here
label_sr_no.setTextSize(15);
srno_table.addView(srno_tr_head, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//---------------Serial no Table Header-----------------------------------------------
report_table=(TableLayout) findViewById(R.id.report_table);
//---------------report Table Header-----------------------------------------------
TableRow report_tr_head = new TableRow(this);
report_tr_head.setId(10);
report_tr_head.setBackgroundResource(R.drawable.list_header);
report_tr_head.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView label_test_name = new TextView(this);
label_test_name.setId(20);
label_test_name.setText("Test Name");
label_test_name.setTextColor(Color.WHITE);
label_test_name.setPadding(5,5,5,5);
report_tr_head.addView(label_test_name);// add the column to the table row here
label_test_name.setTextSize(15);
TextView label_test_date = new TextView(this);
label_test_date.setId(21);// define id that must be unique
label_test_date.setText("Date"); // set the text for the header
label_test_date.setTextColor(Color.WHITE); // set the color
label_test_date.setPadding(5,5,5,5); // set the padding (if required)
report_tr_head.addView(label_test_date); // add the column to the table row here
label_test_date.setTextSize(15);
TextView label_ro = new TextView(this);
label_ro.setId(21);// define id that must be unique
label_ro.setText("R.O."); // set the text for the header
label_ro.setTextColor(Color.WHITE); // set the color
label_ro.setPadding(5,5,5,5); // set the padding (if required)
report_tr_head.addView(label_ro); // add the column to the table row here
label_ro.setTextSize(15);
TextView label_wo = new TextView(this);
label_wo.setId(21);// define id that must be unique
label_wo.setText("W.O."); // set the text for the header
label_wo.setTextColor(Color.WHITE); // set the color
label_wo.setPadding(5,5,5,5); // set the padding (if required)
report_tr_head.addView(label_wo); // add the column to the table row here
label_wo.setTextSize(15);
TextView label_lo = new TextView(this);
label_lo.setId(21);// define id that must be unique
label_lo.setText("L.O."); // set the text for the header
label_lo.setTextColor(Color.WHITE); // set the color
label_lo.setPadding(5,5,5,5); // set the padding (if required)
report_tr_head.addView(label_lo); // add the column to the table row here
label_lo.setTextSize(15);
TextView label_max = new TextView(this);
label_max.setId(21);// define id that must be unique
label_max.setText("Max."); // set the text for the header
label_max.setTextColor(Color.WHITE); // set the color
label_max.setPadding(5,5,5,5); // set the padding (if required)
report_tr_head.addView(label_max); // add the column to the table row here
label_max.setTextSize(15);
TextView label_tm = new TextView(this);
label_tm.setId(21);// define id that must be unique
label_tm.setText("T.M."); // set the text for the header
label_tm.setTextColor(Color.WHITE); // set the color
label_tm.setPadding(5,5,5,5); // set the padding (if required)
report_tr_head.addView(label_tm); // add the column to the table row here
label_tm.setTextSize(15);
TextView label_rank = new TextView(this);
label_rank.setId(21);// define id that must be unique
label_rank.setText("Rank"); // set the text for the header
label_rank.setTextColor(Color.WHITE); // set the color
label_rank.setPadding(5,5,5,5); // set the padding (if required)
report_tr_head.addView(label_rank); // add the column to the table row here
label_rank.setTextSize(15);
report_table.addView(report_tr_head, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//---------------Serial no Table Header-----------------------------------------------
}
//onclick device back button
@Override
public void onBackPressed() {
// do something on back.
Intent intent = new Intent(ReportListActivity.this, TakeTestActivity.class);
finish();
ReportListActivity.this.startActivity(intent);
return;
}
//method to show toast message
public void makeAToast(String str) {
//yet to implement
Toast toast = Toast.makeText(this,str, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
//check connection
public boolean reportList_isOnline() {
ConnectivityManager cm =(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
//sending request for exam name list
new MyAsyncTask_download_report_list().execute(getFromPreference("student_code"),getFromPreference("reportExamType"));
return true;
}
//alert box to show internet connection error
AlertDialog.Builder Internet_Alert = new AlertDialog.Builder(ReportListActivity.this);
// set title
Internet_Alert.setCancelable(false);
Internet_Alert.setTitle("Attention!");
Internet_Alert.setMessage("This application requires internet connectivity, no internet connection detected");
Internet_Alert.setPositiveButton("Quit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1)
{
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
onQuitPressed();
}
});
Internet_Alert.create().show();
return false;
}
//to remove application from task manager
public void onQuitPressed() {
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
}
//saving A VALUE in preference variable
public void saveInPreference(String name, String content)
{
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(name,content);
editor.commit();
}
//getting content from preferences
public String getFromPreference(String variable_name)
{
String preference_return;
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
preference_return = preferences.getString(variable_name,"");
return preference_return;
}
//===================================================================================================================================
//sending student code and exam type to server to report list
//===================================================================================================================================
private class MyAsyncTask_download_report_list extends AsyncTask<String, Integer, Double>{
@Override
protected Double doInBackground(String... params) {
// TODO Auto-generated method stub
postData(params[0],params[1]);
return null;
}
@SuppressWarnings("deprecation")
protected void onPostExecute(Double result){
report_list_xmlContent=reportList_responseBody;
parse_ReportList();
}
protected void onProgressUpdate(Integer... progress){
}
@SuppressWarnings("deprecation")
public void postData(String student_code, String exam_type) {
//to handle connection timeout
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
HttpConnectionParams.setSoTimeout(httpParams, 10000);
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient(httpParams);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(ReportListActivity.this);
final String url_first = preferences.getString("URLFirstPart","");
HttpPost httppost = new HttpPost(url_first+"StudentReport");
try {
// Data that I am sending
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("StudentCode", student_code));
nameValuePairs.add(new BasicNameValuePair("ExamType", exam_type));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
try
{
// Execute HTTP Post Request
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
//getting response body and code from the server
reportList_responseCode=""+ response.getStatusLine().getStatusCode();
reportList_responseBody = EntityUtils.toString(response.getEntity());
}
catch (SocketTimeoutException ex)
{
//showing alert in case of connection timeout
AlertDialog alertDialog = new AlertDialog.Builder(ReportListActivity.this).create();
// Setting Dialog Title
alertDialog.setTitle("Please Note");
// Setting Dialog Message
alertDialog.setMessage("Connection problem, please try later!");
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog closed
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
Log.d("Report list response body: ", reportList_responseBody);
Log.d("Report list response code: ", reportList_responseCode);
}
catch (Throwable t ) {
//Toast.makeText( getApplicationContext(),""+t,Toast.LENGTH_LONG).show();
Log.d("Error Time of Login",t+"");
}
}
}
//===================================================================================================================================
//END sending student code and exam type to server to report list
//===================================================================================================================================
// function to populate report list from xml
void parse_ReportList()
{
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
Document doc = parser.getDomElement(report_list_xmlContent); // getting DOM element
//list object to populate spinner
//List<String> list = new ArrayList<String>();
NodeList nl = doc.getElementsByTagName(KEY_SET);
// looping through all item nodes <item>
for ( int i = 0; i < nl.getLength();i++) {
//----------------Serial no table body------------------------------------------
srno_report_tr_data = new TableRow(this);
srno_report_tr_data.setId(10);
srno_report_tr_data.setBackgroundResource(R.drawable.grey_list_bg);
srno_report_tr_data.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
final TextView sr_no = new TextView(this);
sr_no.setId(20);
sr_no.setText(""+(i+1));
sr_no.setTextColor(Color.BLACK);
sr_no.setPadding(5,5,5,5);
sr_no.setGravity(Gravity.CENTER);
sr_no.setTextSize(12);
srno_report_tr_data.addView(sr_no);// add the column to the table row here
srno_table.addView(srno_report_tr_data, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//----------------Serial no table body------------------------------------------
//----------------Report table body------------------------------------------
report_tr_data=new TableRow(this);
report_tr_data.setId(10);
report_tr_data.setBackgroundResource(R.drawable.grey_list_bg);
report_tr_data.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// creating new HashMap
//HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
//Exam name
final TextView test_name = new TextView(this);
test_name.setId(20);
test_name.setText(parser.getValue(e, KEY_SETNAME));
test_name.setTextColor(Color.BLACK);
test_name.setPadding(5,5,5,5);
test_name.setGravity(Gravity.CENTER);
test_name.setTextSize(12);
report_tr_data.addView(test_name);// add the column to the table row here
//Exam date
final TextView test_date = new TextView(this);
test_date.setId(21);// define id that must be unique
test_date.setText(parser.getValue(e, KEY_EXAMDATE)); // set the text for the header
test_date.setTextColor(Color.BLACK); // set the color
test_date.setPadding(5,5,5,5); // set the padding (if required)
test_date.setGravity(Gravity.CENTER);
test_date.setTextSize(12);
report_tr_data.addView(test_date); // add the column to the table row here
//R.O.
final TextView test_ro = new TextView(this);
test_ro.setId(21);// define id that must be unique
test_ro.setText(parser.getValue(e, KEY_RIGHTMARKS)); // set the text for the header
test_ro.setTextColor(Color.BLACK); // set the color
test_ro.setPadding(5,5,5,5); // set the padding (if required)
test_ro.setGravity(Gravity.CENTER);
test_ro.setTextSize(12);
report_tr_data.addView(test_ro); // add the column to the table row here
//W.O.
final TextView test_wo = new TextView(this);
test_wo.setId(21);// define id that must be unique
test_wo.setText(parser.getValue(e, KEY_WRONGMARKS)); // set the text for the header
test_wo.setTextColor(Color.BLACK); // set the color
test_wo.setPadding(5,5,5,5); // set the padding (if required)
test_wo.setGravity(Gravity.CENTER);
test_wo.setTextSize(12);
report_tr_data.addView(test_wo); // add the column to the table row here
//L.O.
final TextView test_lo = new TextView(this);
test_lo.setId(21);// define id that must be unique
test_lo.setText(parser.getValue(e, KEY_LEFTMARKS)); // set the text for the header
test_lo.setTextColor(Color.BLACK); // set the color
test_lo.setPadding(5,5,5,5); // set the padding (if required)
test_lo.setGravity(Gravity.CENTER);
test_lo.setTextSize(12);
report_tr_data.addView(test_lo); // add the column to the table row here
//MAX
final TextView test_max = new TextView(this);
test_max.setId(21);// define id that must be unique
test_max.setText(parser.getValue(e, KEY_FULLMARKS)); // set the text for the header
test_max.setTextColor(Color.BLACK); // set the color
test_max.setPadding(5,5,5,5); // set the padding (if required)
test_max.setGravity(Gravity.CENTER);
test_max.setTextSize(12);
report_tr_data.addView(test_max); // add the column to the table row here
//TM
final TextView test_tm = new TextView(this);
test_tm.setId(21);// define id that must be unique
test_tm.setText(parser.getValue(e, KEY_OBTAINMARKS)); // set the text for the header
test_tm.setTextColor(Color.BLACK); // set the color
test_tm.setPadding(5,5,5,5); // set the padding (if required)
test_tm.setGravity(Gravity.CENTER);
test_tm.setTextSize(12);
report_tr_data.addView(test_tm); // add the column to the table row here
//RANK
final TextView test_rank = new TextView(this);
test_rank.setId(21);// define id that must be unique
test_rank.setText(parser.getValue(e, KEY_STUDENTRANK)); // set the text for the header
test_rank.setTextColor(Color.BLACK); // set the color
test_rank.setPadding(5,5,5,5); // set the padding (if required)
test_rank.setGravity(Gravity.CENTER);
test_rank.setTextSize(12);
report_tr_data.addView(test_rank); // add the column to the table row here
//EXAM SET ID
final TextView test_exam_set = new TextView(this);
test_exam_set.setId(21);// define id that must be unique
test_exam_set.setText(parser.getValue(e, KEY_EXAMSETID)); // set the text for the header
test_exam_set.setTextSize((float) 0.01);
test_exam_set.setTextColor(Color.TRANSPARENT); // set the color
test_exam_set.setPadding(5,5,5,5); // set the padding (if required)
report_tr_data.addView(test_exam_set); // add the column to the table row here
report_table.addView(report_tr_data, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//----------------------On click table row---------------------------------------
report_tr_data.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//---------------------------------------------------------------------------------------------------
saveInPreference("showReportTab", "ScoreCard");
saveInPreference("reportExamName", test_name.getText().toString());
saveInPreference("reportExamSet", test_exam_set.getText().toString());
Intent intent = new Intent(ReportListActivity.this, ReportDetailsActivity.class);
finish();
ReportListActivity.this.startActivity(intent);
}
});
//----------------------On click table row---------------------------------------
//----------------Report table body------------------------------------------
//deactivating progress bar
progress.dismiss();
}
}
}
The layout file is here
P.S.: Every row is clickable except the header
来源:https://stackoverflow.com/questions/16140735/table-view-look-and-usability-enhancement