我是Android编程的新手。我遇到了以下问题:我有一个主要的活动,我称之为阅读程序(json和图像)。我想为我的活动添加一个无限滚动,所以我写了一个更新屏幕的功能(列表)。现在我需要从ScrollView类中调用它,但我不能声明静态这个方法,因为我应该更改变量的数量。
家庭活动
public class Home extends ActionBarActivity implements OnTaskComplete {
LinearLayout wrapper = null;
Context context = this;
public Bitmap imageHandler;
@Override
public void callBackFunction(Bitmap image) {
imageHandler = image;
}
public class Post{
String id;
String title;
String description;
String release;
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public String getRelease() {
return release;
}
public void setTitle(String title) {
this.title = title;
}
public void setDescription(String description) {
this.description = description;
}
public void setRelease(String release) {
this.release = release;
}
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
}
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//OUTER
RelativeLayout outer = (RelativeLayout)findViewById(R.id.outer);
//SCROLLER
Scroller scroller = new Scroller(this, this);
scroller.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT));
//WRAPPER
wrapper = new LinearLayout(this);
wrapper.setOrientation(LinearLayout.VERTICAL);
outer.addView(scroller);
scroller.addView(wrapper);
list();
}
public void list(){
String result = null;
ArrayList<Post> focusOn = new ArrayList<Post>();
try {
URL address = new URL("http://www.youth-stories.com/api/all.php");
URLDataReader reader = new URLDataReader(context);
result = reader.execute(address).get();
}catch (IOException e){
e.printStackTrace();
} catch(InterruptedException e){
e.printStackTrace();
} catch (ExecutionException e){
e.printStackTrace();
}
try {
JSONObject obj = new JSONObject(result);
String success = (String) obj.getString("success");
JSONArray records = obj.getJSONArray("records");
int start = (Globals.PAGE * Globals.STEP);
int limit = start + Globals.STEP;
//for(int i = 0; i < records.length(); i++) {
for(int i = start ; i < limit; i++) {
Post tmp = new Post();
tmp.setId(records.getJSONObject(i).getString("id"));
tmp.setTitle(records.getJSONObject(i).getString("title"));
tmp.setDescription(records.getJSONObject(i).getString("contents"));
tmp.setRelease(records.getJSONObject(i).getString("data_post"));
focusOn.add(tmp);
}
}catch (JSONException e){
e.printStackTrace();
}
//wrapper
LinearLayout container = wrapper;
ProgressDialog dialog = new ProgressDialog(context);
dialog.setMessage("loading contents, please wait..");
dialog.setCancelable(false);
dialog.show();
for(int i = 0; i < focusOn.size(); i++) {
//item
LinearLayout item = new LinearLayout(getApplicationContext());
String select = focusOn.get(i).getId();
item.setId(new Integer(select));
item.setClickable(true);
//setUp new activity
final Intent intent = new Intent(getApplicationContext(),HomeOnSelect.class);
Bundle bundle = new Bundle();
int id = item.getId();
String strid = new Integer(id).toString();
bundle.putString("id", strid);
bundle.putString("title", focusOn.get(i).getTitle());
bundle.putString("contents", focusOn.get(i).getDescription());
bundle.putString("release", focusOn.get(i).getRelease());
intent.putExtras(bundle);
item.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(intent);
}
});
container.addView(item);
item.setOrientation(LinearLayout.HORIZONTAL);
item.setPadding(0, 40, 0, 40);
item.setGravity(Gravity.CENTER_VERTICAL);
item.setBackgroundResource(R.drawable.postlayout);
//image
ImageView asset = new ImageView(getApplicationContext());
URL address = null;
try {
address = new URL("http://www.youth-stories.com/public/admin/CH_FocusOn/images/" + focusOn.get(i).getId() + "_thumb2.jpg");
URLImageReader reader = new URLImageReader(this, this, asset, dialog, i, focusOn.size());
reader.execute(address);
} catch (MalformedURLException e) {
e.printStackTrace();
}
item.addView(asset);
LinearLayout.LayoutParams imgSettings = new LinearLayout.LayoutParams(300, 300);
asset.setLayoutParams(imgSettings);
asset.setPadding(50,0,0,0);
//inside
LinearLayout contents = new LinearLayout(getApplicationContext());
contents.setOrientation(LinearLayout.VERTICAL);
contents.setPadding(55, 0, 100, 0);
item.addView(contents);
//title
TextView title = new TextView(getApplicationContext());
title.setText(focusOn.get(i).getTitle());
title.setTextAppearance(this, R.style.title);
contents.addView(title);
//description
TextView description = new TextView(getApplicationContext());
description.setText(focusOn.get(i).getDescription());
description.setTextAppearance(this, R.style.description);
contents.addView(description);
//date
TextView date = new TextView(getApplicationContext());
date.setText(focusOn.get(i).getRelease());
date.setTextAppearance(this, R.style.description);
contents.addView(date);
//div
LinearLayout div = new LinearLayout(getApplicationContext());
div.setLayoutParams(new LinearLayout.LayoutParams(200, 40));
div.setBackgroundColor(Color.parseColor("#00000000"));
container.addView(div);
}
}
}
SCROLLER CLASS
public class Scroller extends ScrollView {
Context cnt = null;
Activity activity = null;
public Scroller(Context context, Activity activity) {
super(context);
cnt = context;
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
//TOP
if (t == 0) {
//Toast.makeText(cnt, "top", Toast.LENGTH_SHORT).show();
}
//BOTTOM
View view = (View) getChildAt(getChildCount() - 1);
int diff = (view.getBottom() - (getHeight() + getScrollY() + view.getTop()));
if (diff == 0) {
Globals.PAGE++;
Toast.makeText(cnt, "bottom", Toast.LENGTH_SHORT).show();
Resources
}
}
}
答案 0 :(得分:0)
如果你想在你的应用程序中进行无限滚动,可以通过ListView实现它,这里有一些如何在Android上制作无限滚动ListView的教程
LINK 1:
LINK 2:
http://www.survivingwithandroid.com/2013/10/android-listview-endless-adapter.html
答案 1 :(得分:0)
解决: 主要活动中实现的界面:
public void onScrollChanged(Scroller scrollView, int x, int y, int oldx, int oldy) {
// Toast.makeText(this,"sl",Toast.LENGTH_LONG).show();
list();
}
接口:
public interface ScrollViewListener {
void onScrollChanged(Scroller scrollView, int x, int y, int oldx, int oldy);
}
Scrollview课程:
public class Scroller extends ScrollView {
private ScrollViewListener scrollViewListener = null;
public Scroller(Context context, ScrollViewListener mlistener) {
super(context);
this.scrollViewListener = mlistener;
}
public Scroller(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public Scroller(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setScrollViewListener(ScrollViewListener scrollViewListener) {
this.scrollViewListener = scrollViewListener;
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
//TOP
if (t == 0) {
//Toast.makeText(cnt, "top", Toast.LENGTH_SHORT).show();
}
//BOTTOM
View view = (View) getChildAt(getChildCount() - 1);
int diff = (view.getBottom() - (getHeight() + getScrollY() + view.getTop()));
if (diff == 0) {
Globals.PAGE++;
//Toast.makeText(cnt, "bottom", Toast.LENGTH_SHORT).show();
scrollViewListener.onScrollChanged(this, l, t, oldl, oldt);
}
}
}