所以我遇到了一个非常奇怪的问题,我有一个活动,该活动在具有自定义ListView的ListView中显示用户的帖子和日期,从服务器获取并在 onCreate 上填充它们。
所以发生的事情是,每当我开始活动时,都会选择edittext部分,并且通常会出现键盘
我搜索了如何在启动时不聚焦EditText 通过在父布局上将这两行添加到专注于EditText的查找上,可以避免
android:focusable="true"
android:focusableInTouchMode="true"
所以我将其添加到活动的RelativeLayout xml中,这是上面图片的xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/verticalLinear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
tools:context=".UserHome">
<EditText
android:id="@+id/tweetText"
android:layout_width="match_parent"
android:layout_height="122dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="Write Your Tweet"
android:inputType="textPersonName" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/userListHome"
android:layout_alignParentEnd="true"
android:onClick="postTweetFunction"
android:text="Post" />
<ListView
android:id="@+id/userListHome"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_below="@+id/tweetText"
android:focusable="true"
android:focusableInTouchMode="true"/>
</RelativeLayout>
现在我要在上面说一下问题部分,以使您了解到底是什么引起了奇怪的问题。
,直到选择了extext并出现键盘后,列表视图才会出现
我确定列表视图正在填充并且自定义列表视图正在运行
,但是直到我选择editext并出现键盘时,listview内容才会出现,就像listview在选择editext之前一直处于休眠状态。
在该活动中看到,并且未选择editext,因此即使您填充了列表视图,也不会显示。
,但是当我单击他editedit并立即弹出键盘时,就像唤醒了睡眠列表视图一样
并且列表视图和适配器不是匿名的,我已经定义了它们 这是活动代码,如果需要参考的话
public class UserHome extends AppCompatActivity {
ListView listView;
ArrayList<String> userOwnTweets;
ArrayList<String> userOwnTweetsDate;
ArrayList<String> getUserOwnTweetsObjectId;
EditText tweetText;
CustomArrayAdapter customArrayAdapter;
Typeface custom_font1, custom_font2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_home);
custom_font1 = Typeface.createFromAsset(getAssets(), "fonts/gooddog.otf");
custom_font2 = Typeface.createFromAsset(getAssets(), "fonts/quicksand.otf");
listView = (ListView) findViewById(R.id.userListHome);
tweetText = (EditText) findViewById(R.id.tweetText);
userOwnTweets = new ArrayList<String>();
userOwnTweetsDate = new ArrayList<String>();
getUserOwnTweetsObjectId = new ArrayList<String>();
customArrayAdapter = new CustomArrayAdapter(getApplicationContext());
listView.setAdapter(customArrayAdapter);
// updating the tweets onCreate
updateTweetList();
}
public void updateTweetList(){
// clearing the lists to avoid duplicate entries
userOwnTweets.clear();
userOwnTweetsDate.clear();
getUserOwnTweetsObjectId.clear();
// just backend code to get the tweets of the user
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Tweet");
query.whereEqualTo("username", ParseUser.getCurrentUser().getUsername());
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null && objects.size() > 0){
for (ParseObject object : objects){
String tempString = object.getString("tweet");
tempString = tempString.substring(0, Math.min(tempString.length(), 20));
tempString+="...";
// adding them to the arraylists
userOwnTweets.add(tempString);
userOwnTweetsDate.add(object.getString("date"));
getUserOwnTweetsObjectId.add(object.getObjectId());
}
}
}
});
// updating the customlistview for datastatechanged
customArrayAdapter.notifyDataSetChanged();
}
@Override
public void onBackPressed() {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
}
public void postTweetFunction(View view){
// adding a new tweet and refreshing the tweet list from teh server
ParseObject userTwitter = new ParseObject("Tweet");
userTwitter.put("tweet", tweetText.getText().toString());
userTwitter.put("username", ParseUser.getCurrentUser().getUsername());
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
userTwitter.put("date", String.valueOf(dateFormat.format(new Date())));
userTwitter.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
Toast.makeText(getApplicationContext(), "Tweet Added", Toast.LENGTH_SHORT).show();
tweetText.setText("");
updateTweetList();
}
}
});
hideKeyBoard();
}
public void hideKeyBoard(){
// hiding the keyboard
try {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
catch (Exception e){
e.printStackTrace();
}
}
// the custom adapter
class CustomArrayAdapter extends BaseAdapter {
Context context;
LayoutInflater layoutInflater;
// Constructor
public CustomArrayAdapter(Context context) {
this.context = context;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return userOwnTweets.size();
}
@Override
public Object getItem(int position) {
return userOwnTweets.get(position);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = layoutInflater.inflate(R.layout.row_layout, null);
TextView userHomeTweet = (TextView) view.findViewById(R.id.userHomeTweet);
TextView userHomeTweetdate = (TextView) view.findViewById(R.id.userHomeTweetDate);
userHomeTweet.setTypeface(custom_font1);
userHomeTweetdate.setTypeface(custom_font2);
try {
userHomeTweet.setText(userOwnTweets.get(i));
userHomeTweetdate.setText(userOwnTweetsDate.get(i));
}catch(Exception e){
e.printStackTrace();
}
// random color for each
int color = Color.LTGRAY;
view.setBackgroundColor(color);
return view;
}
}
}
我已经使用了适当的注释,代码很简单,我只是在MongoDB上使用了服务器,我已经对其进行了注释。
这是customlistview布局xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/userHomeTweet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="40dp"
/>
<TextView
android:id="@+id/userHomeTweetDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="49dp"
android:textSize="20dp" />
</RelativeLayout>
每个listview元素内部都有一个this布局,因此使其成为customlistview
这就是我尽可能清楚的所有内容,但是问我是否错过了可能对解决问题有所帮助的任何事情
所以您能告诉我,为什么直到我选择EditText和出现Keyboard时listview才会出现,这似乎是一个奇怪的问题,请向我指出正确的方向。谢谢。
答案 0 :(得分:1)
尝试一下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/verticalLinear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
tools:context=".UserHome">
<EditText
android:id="@+id/tweetText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_toStartOf="@id/button"
android:ems="10"
android:hint="Write Your Tweet"
android:inputType="textPersonName" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/userListHome"
android:layout_alignParentEnd="true"
android:onClick="postTweetFunction"
android:text="Post" />
<ListView
android:id="@+id/userListHome"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tweetText"
android:layout_alignParentBottom="true" />
</RelativeLayout>
编辑
public void updateTweetList(){
// clearing the lists to avoid duplicate entries
userOwnTweets.clear();
userOwnTweetsDate.clear();
getUserOwnTweetsObjectId.clear();
// just backend code to get the tweets of the user
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Tweet");
query.whereEqualTo("username", ParseUser.getCurrentUser().getUsername());
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null && objects.size() > 0){
for (ParseObject object : objects){
String tempString = object.getString("tweet");
tempString = tempString.substring(0, Math.min(tempString.length(), 20));
tempString+="...";
// adding them to the arraylists
userOwnTweets.add(tempString);
userOwnTweetsDate.add(object.getString("date"));
getUserOwnTweetsObjectId.add(object.getObjectId());
}
// updating the customlistview for datastatechanged
customArrayAdapter.notifyDataSetChanged();
}
}
});
}