我遇到了在Popup中滚动ListView的问题。我有一个具有对话框主题的活动,以便它可以作为弹出窗口打开。 Popup应在底部包含EditText,其上方的剩余区域应为ListView。当活动开始时,它应该从键盘打开&专注于EditText。
问题:当键盘打开时,ListView不会滚动。
以下是我的活动代码:
public class CommentsActivity extends Activity{
private InputMethodManager imm;
private String commentCount;
private ShopPirateDatabase db;
private ArrayList<CommentsBean> commentsList = new ArrayList<CommentsBean>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.comments_layout);
imm = (InputMethodManager) this
.getSystemService(Context.INPUT_METHOD_SERVICE);
db=new ShopPirateDatabase(this);
if(getIntent().hasExtra("count")){
commentCount=getIntent().getExtras().getString("count");
}
ListView lvMyComments=(ListView)findViewById(R.id.lvMyComments);
lvMyComments.setVisibility(View.GONE);
final Button btnPost=(Button)findViewById(R.id.btnPost);
btnPost.setVisibility(View.GONE);
etAddComment=(EditText)findViewById(R.id.etAddComment);
RelativeLayout progressLayout=(RelativeLayout)findViewById(R.id.progressLayout);
progressLayout.setVisibility(View.VISIBLE);
TextView tvNoComments=(TextView)findViewById(R.id.tvNoComments);
tvNoComments.setVisibility(View.GONE);
imm.showSoftInput(etAddComment, InputMethodManager.SHOW_IMPLICIT);
if(Integer.parseInt(commentCount) > 0){
getCommentsFromDb();
if(commentsList!=null && commentsList.size() > 0){
progressLayout.setVisibility(View.GONE);
tvNoComments.setVisibility(View.GONE);
lvMyComments.setVisibility(View.VISIBLE);
CommentsAdapter commentsAdapter = new CommentsAdapter(
CommentsActivity.this, commentsList);
lvMyComments.setAdapter(commentsAdapter);
}
} else{
progressLayout.setVisibility(View.GONE);
tvNoComments.setVisibility(View.VISIBLE);
lvMyComments.setVisibility(View.GONE);
}
}
private void getCommentsFromDb() {
db.openDatabase();
Cursor c = db.getComments();
if (c != null) {
commentsList = null;
commentsList = new ArrayList<CommentsBean>();
if (c.moveToFirst()) {
do {
CommentsBean mBeanClass = new CommentsBean();
mBeanClass
.setCommentText(c.getString(c
.getColumnIndex(db.KEY_COMMENT_TEXT)));
mBeanClass
.setCommentedDate(c.getString(c
.getColumnIndex(db.KEY_COMMENT_DATE)));
mBeanClass
.setCommentedBy(c.getString(c
.getColumnIndex(db.KEY_COMMENT_USER)));
commentsList.add(mBeanClass);
} while (c.moveToNext());
}
}
c.close();
db.closeDatabase();
}
}
comments_layout.xml
<RelativeLayout
android:id="@+id/commentsBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="@dimen/margin_5"
android:layout_marginLeft="@dimen/margin_2"
android:layout_marginRight="@dimen/margin_5" >
<Button
android:id="@+id/btnPost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="@dimen/margin_35"
android:background="@color/coupon_color"
android:paddingBottom="@dimen/margin_7"
android:paddingLeft="@dimen/margin_15"
android:paddingRight="@dimen/margin_15"
android:paddingTop="@dimen/margin_7"
android:layout_alignParentRight="true"
android:layout_marginRight="@dimen/margin_2"
android:text="Post"
android:visibility="gone"
android:textColor="@color/white"
android:textSize="@dimen/normal_textsize" />
<EditText
android:id="@+id/etAddComment"
android:layout_width="match_parent"
android:layout_alignParentLeft="true"
android:layout_marginLeft="@dimen/margin_2"
android:layout_toLeftOf="@id/btnPost"
android:layout_marginRight="@dimen/margin_2"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/margin_5"
android:hint="@string/comment_hint" />
</RelativeLayout>
<ListView
android:id="@+id/lvMyComments"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginBottom="@dimen/margin_5"
android:divider="@color/home_bg_color"
android:dividerHeight="@dimen/divider_height"
android:scrollbars="none"
android:visibility="gone" >
</ListView>
<RelativeLayout
android:id="@+id/progressLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/commentsBox"
android:layout_marginLeft="@dimen/margin_10"
android:layout_marginRight="@dimen/margin_10" >
<ProgressBar
android:id="@+id/pBarComments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:indeterminate="true"
android:indeterminateDrawable="@drawable/circular_progress_bar" />
<TextView
android:id="@+id/tvWaitComments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/pBarComments"
android:layout_centerHorizontal="true"
android:layout_marginLeft="@dimen/margin_5"
android:layout_marginRight="@dimen/margin_5"
android:text="Loading comments..."
android:textColor="@color/black"
android:textSize="@dimen/small_textsize" />
</RelativeLayout>
<TextView
android:id="@+id/tvNoComments"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/etAddComment"
android:layout_marginLeft="@dimen/margin_15"
android:layout_marginRight="@dimen/margin_25"
android:gravity="center_vertical|center_horizontal"
android:text="No comments have been posted for this offer"
android:textColor="@color/black"
android:textSize="@dimen/normal_textsize"
android:visibility="gone" />
CommentsAdapter.java
public class CommentsAdapter extends BaseAdapter {
private Context context;
private ArrayList<CommentsBean> mArrayList = new ArrayList<CommentsBean>();
public CommentsAdapter(Context ctx, ArrayList<CommentsBean> arrayList) {
this.context = ctx;
this.mArrayList = arrayList;
}
@Override
public int getCount() {
return mArrayList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view;
view = null;
convertView = null;
if (convertView == null) {
view = new View(context);
view = inflater.inflate(R.layout.list_comments, null);
TextView commentText = (TextView) view
.findViewById(R.id.commentText);
TextView postedDate = (TextView) view.findViewById(R.id.postedDate);
TextView userName = (TextView) view.findViewById(R.id.userName);
commentText.setText(mArrayList.get(position).getCommentText());
userName.setText(" - " + mArrayList.get(position).getCommentedBy());
String dt = mArrayList.get(position).getCommentedDate();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = null;
try {
d = sdf.parse(dt);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy");
String convertedDate = null;
convertedDate = sdf1.format(d);
postedDate.setText(convertedDate);
}
return view;
}
}
list_comments.xml
<ImageView android:id="@+id/cmtIcon"
android:layout_width="@dimen/margin_25"
android:layout_height="@dimen/margin_25"
android:src="@drawable/comment_list_icon"
android:layout_alignParentTop="true"
android:layout_marginTop="@dimen/margin_5"
android:layout_marginLeft="@dimen/margin_5"
android:layout_alignParentLeft="true" />
<TextView
android:id="@+id/commentText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/margin_10"
android:layout_toRightOf="@id/cmtIcon"
android:layout_alignParentTop="true"
android:layout_marginTop="@dimen/margin_5"
android:textColor="@color/black"
android:textSize="@dimen/normal_textsize" />
<TextView
android:id="@+id/postedDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/commentText"
android:layout_toRightOf="@id/cmtIcon"
android:layout_marginLeft="@dimen/margin_10"
android:layout_marginTop="@dimen/margin_5"
android:text="Posted on"
android:textColor="@color/black"
android:textSize="@dimen/small_textsize" />
<TextView
android:id="@+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/postedDate"
android:layout_marginTop="@dimen/margin_3"
android:text="User Name"
android:layout_below="@id/commentText"
android:textColor="@color/black"
android:textSize="@dimen/normal_textsize" />